KeyListener, keyPressed versus keyTyped
Solution 1
keyPressed - when the key goes down
keyReleased - when the key comes up
keyTyped - when the unicode character represented by this key is sent by the keyboard to system input.
I personally would use keyReleased for this. It will fire only when they lift their finger up.
Note that keyTyped will only work for something that can be printed (I don't know if F5 can or not) and I believe will fire over and over again if the key is held down. This would be useful for something like... moving a character across the screen or something.
Solution 2
Neither. You should NOT use a KeyLIstener.
Swing was designed to be used with Key Bindings. Read the section from the Swing tutorial on How to Use Key Bindings.
CodeGuy
Updated on March 31, 2020Comments
-
CodeGuy about 4 years
I have a JFrame (well, a class which extends JFrame) and I want to do an action when I press the F5 key. So, I made the class implement KeyListener. And with that, came three methods, keyPressed, keyReleased, and keyTyped.
Which of these methods should I use to listen for F5 being pressed? keyPressed or keyTyped? I currently have the following, however it does not print anything when I press F5.
public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_F5) System.out.println("F5 pressed"); } public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub }
-
CodeGuy over 12 yearsHi, thank you for your answer. Please see my revised question
-
corsiKa over 12 yearsIf you're not getting anything from that, then it's probably because you forgot to add your listener. If you don't have a
addListener
method call anywhere, you need to add one. If you post some code I could assist with this. -
CodeGuy over 12 yearsyup, forgot to add the listener
-
corsiKa over 12 yearsJust for the record, I typically use
keyReleased
instead ofkeyPressed
so that if they didn't really want to do whatever they're doing, they can hold the key down instead of letting it go, and press escape. I then keep a boolean inside of whether or not escape is down (on key pressed if it's escape, it sets the boolean true, and on key released if it's escape, sets the boolean false) and i don't let them do any other actions if escape is pressed. Just some food for thought. Cheers -
Joshua over 12 yearsGood advice. I'm a convert and should have known better before ;)
-
Leigh almost 12 yearsPlease do not use text message abbreviations. Also, this question was answered some time ago. Its best not to resurrect old thread unless the response adds something significant that was missing from previous answers.
-
Eric Leschinski about 7 yearsThere should be at least one or two sentences accompanying every code snippet explaining what part of the question you are addressing.
-
Leo Schoenborn about 7 yearsAs suggested above, it is the keyListener() that listens for a key event. Precede the code that responds to keyPressed() with addKeyListener(). I included a snippet of code to serve as an example that you can modify.
-
Lealo over 6 years...And unicodes is done by pressing and holding down ALT while typing the character code with the numeric keypad on the right. Thats why I never could listen for them with java before...