You are receiving an event for KEY_PRESSED and KEY_RELEASED.
You need to check if, in addition to the KeyEvent
having the correct KeyCode you want to make sure it is the correct action, ie KEY_PRESSSED. One way to fix this is add a check for the action before you go into your switch statement.
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(final AWTEvent event) {
if (event.getKeyChar() == KeyEvent.KEY_PRESSED)
{
final KeyEvent evt = (KeyEvent) event;
switch(event.getKeyCode()){
//switch statement code
}
}
}, AWTEvent.KEY_EVENT_MASK);
Another thing you could do is create a KeyEventDispatcher
and add it to the KeyboardFocusManager
like this:
//create KeyEventDispatcher myKeyEventDispatcher
KeyboardFocusManager focusManager = KeyboardFocusManager.
getCurrentKeyboardFocusManager();
focusManager.addKeyEventDispatcher(myKeyEventDispatcher);
That way you will only get key event and you can dispatch them yourself