For a game, I use a KeyListener
to know when a key is pressed down.
public synchronized void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
keyRightIsDown = true;
}
}
public synchronized void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
keyRightIsDown = false;
}
}
This works 99.9%. But from time to time (often enough), the keyReleased
is not called, when the key is released (causing the game character to continue moving right - pressing the key again fixes the problem).
[Maybe relevant:] I use OSX 10.6 and I press down multiple keys at the same time quiet often.
How can I make this work 100% ?