hi! I can’t tell you if the JInput handles keyboard, because I use JInput for the Gamepad handler. But keyboard is really simple to handle with the common AWT dispatcher. But as far as I tested AWT with keyboard, I’ve experienced multiple issues, especially with the key-pressed EVENT. It can be dispatched repeatly or once. Anyway, handling correctly a keyboard event is notifying accurately WHEN and WHERE the event is handled. have a look at the KeyEventDispatcher interface and you can notice two things : the returned BOOLEAN and the KeyboardFocusManager that it is handling the impl. interface.
class MyKeybardHandler implements KeyEventDispatcher {
// registry for the dispatched EventObject's
SortedMap<Long, Integer> events = Collections.synchronizedSortedMap(new TreeMap<Long, Integer>());
public boolean dispatchKeyEvent(KeyEvent e) {
// usually, we handle a KeyEvent over a switch callback
switch(e.getID()) {
case KeyEvent.KEY_PRESSED:
events.put(e.getWhen(), e.getKeyCode());
return true;
case KeyEvent.KEY_RELEASED:
if(events.containsValue(e.getKeyCode())) {
synchronized(events) {
for(Iterator<Long> it = events.values().iterator(); i.hasNext();) { if(e.getKeyCode() == i.next()) i.remove(); }
}
return true;
} break;
default:
break;
} return false;
}
}
adding this class to a KeyboardFocusManager and you can detect the pressed keys on your keyboard. It is important to “focus” on the real time processing events, not the processed event realizing timing.
If you want KeyEvent.KEY_TYPED events, you must be aware that that type of event is SEPARATELY dispatched by AWT, thus it must be handled seperately from the two types above, eventually as a separate Thread.