Reputation: 393
I am having some issues getting some code to work that detects release of a keystroke. I am writing a video game, and I would like to iniitate the movement of an icon with the press of a key, and terminate the movement once the key has been released. I have read other people's postings on this, and they seem to say the way to handle this is to set a flag in the keyReleased code that would terminate the movement I assume you would have initiated in the keyPressed code. However, I assume you would have to put a Thread.Sleep into the KeyPressed code in order to allow the KeyReleased code to fire (which would set the flag)? Otherwise control would never return to the dispatch loop from the KeyPressed code?
Anyway, here is the code I can not get to work that should pickup the release of key 'a'.
MoveGunLeftActionReleased aKeyActionReleased= new MoveGunLeftActionReleased();
…
…
statusPanel.getInputMap().put(KeyStroke.getKeyStroke("released a"), "aCharKeyReleased");
statusPanel.getActionMap().put("aCharKeyReleased", aKeyActionReleased);
… …
static class MoveGunLeftActionReleased extends AbstractAction
{
public void actionPerformed(ActionEvent rf) {
System.out.println("RELEASED");
aKeyReleased= true;
}
}
The object: aKeyActionReleased never executes.
What am I doing wrong?
Upvotes: 0
Views: 200