I am currently testing the KeyEventDispatcher
.
Therefore I wrote a little JFrame
which implements the KeyEventDispatcher
and my own
keyPressed
and keyReleased
Methods.
In those Methods, I am using a flag based System to detect only the first keypress of every arrow key.
Everything works, if you click the keys separately. But if you click Right, Up, Left (without releasing any of them) the Left key won't be recognized.
Console output:
Right clicked
4
Up clicked
6
Expected output:
Right clicked
4
Up clicked
6
Left clicked
7
My code is the following:
Main class:
public class Main {
public static void main(String[] args) {
UI m = new UI();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(m);
}
}
UI class:
public class UI extends JFrame implements KeyEventDispatcher{
short lurd = 0;
enum KEYSTATES{
LEFT(1),
UP(2),
RIGHT(4),
DOWN(8);
private int m_val;
KEYSTATES(int val){
m_val = val;
}
public int getm_val(){
return m_val;
}
}
public UI(){
setSize(800,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
switch(e.getID())
{
case KeyEvent.KEY_PRESSED:
keyPressed(e);
return true;
case KeyEvent.KEY_RELEASED:
keyReleased(e);
return true;
}
return false;
}
private void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT && ((lurd & KEYSTATES.LEFT.getm_val()) != KEYSTATES.LEFT.getm_val())){
lurd |= KEYSTATES.LEFT.getm_val();
System.out.println("Left clicked");
System.out.println(lurd);
}
else if(e.getKeyCode() == KeyEvent.VK_UP && ((lurd & KEYSTATES.UP.getm_val()) != KEYSTATES.UP.getm_val()))
{
lurd |= KEYSTATES.UP.getm_val();
System.out.println("Up clicked");
System.out.println(lurd);
}
else if(e.getKeyCode() == KeyEvent.VK_RIGHT && ((lurd & KEYSTATES.RIGHT.getm_val()) != KEYSTATES.RIGHT.getm_val()))
{
lurd |= KEYSTATES.RIGHT.getm_val();
System.out.println("Right clicked");
System.out.println(lurd);
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN && ((lurd & KEYSTATES.DOWN.getm_val()) != KEYSTATES.DOWN.getm_val()))
{
lurd |= KEYSTATES.DOWN.getm_val();
System.out.println("Down clicked");
System.out.println(lurd);
}
}
private void keyReleased(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT && ((lurd & KEYSTATES.LEFT.getm_val()) == KEYSTATES.LEFT.getm_val())){
lurd &= ~KEYSTATES.LEFT.getm_val();
System.out.println("Left released");
System.out.println(lurd);
}
else if(e.getKeyCode() == KeyEvent.VK_UP && ((lurd & KEYSTATES.UP.getm_val()) == KEYSTATES.UP.getm_val()))
{
lurd &= ~KEYSTATES.UP.getm_val();
System.out.println("Up released");
System.out.println(lurd);
}
else if(e.getKeyCode() == KeyEvent.VK_RIGHT && ((lurd & KEYSTATES.RIGHT.getm_val()) == KEYSTATES.RIGHT.getm_val()))
{
int x = ~KEYSTATES.RIGHT.getm_val();
lurd &= x;
System.out.println("Right released");
System.out.println(lurd);
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN && ((lurd & KEYSTATES.DOWN.getm_val()) == KEYSTATES.DOWN.getm_val()))
{
lurd &= ~KEYSTATES.DOWN.getm_val();
System.out.println("Down released");
System.out.println(lurd);
}
}
}