Reputation: 123
I'm implementing a KeyListener, and trying to find out whether a key is being held down in Java, as opposed to just being pressed and released. Is there a way I can do this?
Upvotes: 0
Views: 69
Reputation: 102812
Yes. For your implementation of KeyListener...
The keyPressed
method is called when the key is depressed, and the keyReleased
method is called when the key is released, and this works for almost any key (including CTRL or CMD; generally keys like 'fn' on laptops can't be detected but that's about it).
The keyTyped
method shouldn't be used if you want this level of fine-tuning; keyTyped is fired when a key is pressed AND released, and will also repeat if the key is held down depending on OS. So don't use that one.
Upvotes: 1