today I added keyboard input to my game the way I always do, with KeyListener
, but for some reason, in my program whenever I click outside of the window and click back in, the key input just stops working. Here is what I have in my keyPressed
and keyReleased
methods:
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_W:
keys[0] = true;
break;
case KeyEvent.VK_D:
keys[1] = true;
break;
case KeyEvent.VK_S:
keys[2] = true;
break;
case KeyEvent.VK_A:
keys[3] = true;
break;
}
}
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_W:
keys[0] = false;
break;
case KeyEvent.VK_D:
keys[1] = false;
break;
case KeyEvent.VK_S:
keys[2] = false;
break;
case KeyEvent.VK_A:
keys[3] = false;
break;
}
}