I’m attempting to write a dynamic keybind system for a java game. It mostly works, except for one huge problem: characters like a, b ,c don’t work. Only characters like ~, &, $ work.
I call the method with KeyFinder lookup = KeyFinder.lookup(keyChar);
Here is my current code file, sorry if this is really long.
KeyFinder.java
public enum KeyFinder {
A(Keyboard.KEY_A, "a", "A"),
B(Keyboard.KEY_B, "b", "B"),
C(Keyboard.KEY_C, "c", "C"),
D(Keyboard.KEY_D, "d", "D"),
E(Keyboard.KEY_E, "e", "E"),
F(Keyboard.KEY_F, "f", "F"),
G(Keyboard.KEY_G, "g", "G"),
H(Keyboard.KEY_H, "h", "H"),
I(Keyboard.KEY_I, "i", "I"),
J(Keyboard.KEY_J, "j", "J"),
K(Keyboard.KEY_K, "k", "K"),
L(Keyboard.KEY_L, "l", "L"),
M(Keyboard.KEY_M, "m", "M"),
N(Keyboard.KEY_N, "n", "N"),
O(Keyboard.KEY_O, "o", "O"),
P(Keyboard.KEY_P, "p", "P"),
Q(Keyboard.KEY_Q, "q", "Q"),
R(Keyboard.KEY_R, "r", "R"),
S(Keyboard.KEY_S, "s", "S"),
T(Keyboard.KEY_T, "t", "T"),
U(Keyboard.KEY_U, "u", "U"),
V(Keyboard.KEY_V, "v", "V"),
W(Keyboard.KEY_W, "w", "W"),
X(Keyboard.KEY_X, "x", "X"),
Y(Keyboard.KEY_Y, "y", "Y"),
Z(Keyboard.KEY_Z, "z", "Z"),
TILDE(Keyboard.KEY_GRAVE, "~", "`"),
TAB(Keyboard.KEY_TAB, "tab"),
F1(Keyboard.KEY_F1, "F1"),
F2(Keyboard.KEY_F2, "F2"),
F3(Keyboard.KEY_F3, "F3"),
F4(Keyboard.KEY_F4, "F4"),
F5(Keyboard.KEY_F5, "F5"),
F6(Keyboard.KEY_F6, "F6"),
F7(Keyboard.KEY_F7, "F7"),
F8(Keyboard.KEY_F8, "F8"),
F9(Keyboard.KEY_F9, "F9"),
F10(Keyboard.KEY_F10, "F10"),
F11(Keyboard.KEY_F11, "F11"),
F12(Keyboard.KEY_F12, "F12"),
F13(Keyboard.KEY_F13, "F13"),
F14(Keyboard.KEY_F14, "F14"),
F15(Keyboard.KEY_F15, "F15"),
ONE(Keyboard.KEY_1, "1", "!", "one"),
TWO(Keyboard.KEY_2, "2", "@", "two"),
THREE(Keyboard.KEY_3, "3", "#", "three"),
FOUR(Keyboard.KEY_4, "4", "$", "four"),
FIVE(Keyboard.KEY_5, "5", "%", "five"),
SIX(Keyboard.KEY_6, "6", "^", "six"),
SEVEN(Keyboard.KEY_7, "7", "&", "seven"),
EIGHT(Keyboard.KEY_8, "8", "*", "eight"),
NINE(Keyboard.KEY_9, "9", "(", "nine"),
ZERO(Keyboard.KEY_0, "0", ")", "zero"),
MINUS(Keyboard.KEY_MINUS, "-", "_", "minus"),
EQUALS_ADD(Keyboard.KEY_EQUALS, "=", "+", "plus", "equals"),
INSERT(Keyboard.KEY_INSERT, "insert", "ins"),
DEL(Keyboard.KEY_DELETE, "del", "delete"),
HOME(Keyboard.KEY_HOME, "home"),
ADD(Keyboard.KEY_ADD, "add"),
PAGE_UP(Keyboard.KEY_PRIOR, "prior", "pageup"),
PAGE_DOWN(Keyboard.KEY_NEXT, "next", "pagedown"),
NUM_LOCK(Keyboard.KEY_NUMLOCK, "numlock", "numberlock"),
SQBRACKET_LEFT(Keyboard.KEY_LBRACKET, "[", "{"),
SQBRACKET_RIGHT(Keyboard.KEY_RBRACKET, "]", "}"),
SEMICOLON(Keyboard.KEY_SEMICOLON, ";", ":");
private static final Map<Integer, KeyFinder> keys = new HashMap<Integer, KeyFinder>();
private static final Map<String, KeyFinder> lookup = new LinkedHashMap<String, KeyFinder>();
private final int key;
private final String keyName;
private final String[] lookupKeys;
static {
for (KeyFinder type : EnumSet.allOf(KeyFinder.class)) {
keys.put(type.key, type);
for (String key : type.lookupKeys) {
lookup.put(key, type);
}
}
}
KeyFinder(int key, String keyName, String lookupKey) {
this.key = key;
this.keyName = keyName;
this.lookupKeys = new String[] { lookupKey };
}
KeyFinder(int key, String keyName, String... lookupKeys) {
this.key = key;
this.keyName = keyName;
this.lookupKeys = lookupKeys;
}
public static KeyFinder fromKey(int key) {
return keys.get(key);
}
public static String toKeyName(int key) {
KeyFinder type = keys.get(key);
if (type != null) {
return type.getKeyName();
} else {
return "#" + key;
}
}
public static KeyFinder lookup(String keyName) {
return lookup(keyName, true);
}
public static KeyFinder lookup(String keyName, boolean fuzzy) {
KeyFinder i = StringUtil.lookup(lookup, keyName, fuzzy);
if(i != null)
return i;
else
try {
return fromKey(Integer.parseInt(keyName));
} catch (NumberFormatException e) {
return null;
}
}
public int getKey() {
return key;
}
public String getKeyName() {
return keyName;
}
}
I stripped down your
KeyFinderand tested together with theStringUtiland realize that you never ever use thekeyNamein yourlookupmap. Soais not there.Here are some changes I did which might help you.
Change the
lookupKeysto be a list instead of an array.Remove one of your constructors (it is obsolete since you have one with
varargs)Change the other constructor and let
keyNamebe included in thelookupKeys.It worked for me in some simple tests.
NB
I have not analyzed your code more than this. I don’t know if you actually need the
keyNamebut I leave that up to you. I just wanted the lookup to work.