input - Java lwjgl keyboard handler -
i've created class takes lwjgl's keyboard input , turns list of strings keys being pressed.
public class keyhandler { arraylist<string> keyspressed; public keyhandler() { keyspressed = new arraylist<string>(); } public void checkkeys() { while (keyboard.next()) { string keystring = keyboard.getkeyname(keyboard.geteventkey()); if (!keyspressed.contains(keystring)) { keyspressed.add(keystring); // key has been pressed } else { keyspressed.remove(keystring); // key has been released } } } public void runkeys() { if (keyspressed.size() > 0) { (string str : keyspressed) { system.out.println("key handler got key:" + str); // run class key } } else { // no keys have been pressed } } }
i'm trying figure out way 'runkeys' run class name, example.
w.java
public class w { public static void exc() { player.movez(10); } }
the reason doing avoid having run through 50+ if statements check input
- create
map<character, consumer<character>>
. - create class each character implements
consumer<character>
interface. - store objects of classes in map.
- call consume on appropriate consumer object map using detected keystroke character.
hope helps.
Comments
Post a Comment