I have searched about difference between KeyPressed
and KeyTyped
Events but still I'm not clear about that . One thing I have found is Keypressed is triggered first than KeyTyped .
Please clarify me when these are triggered exactly . Which is appropriate to use for which purpose ?
Thanks in advance

- 168,117
- 40
- 217
- 433

- 173
- 1
- 4
- 12
-
Is this using Swing or AWT components? For Swing, typically use key bindings over the AWT based, lower level, `KeyListener`. See [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for details on how to use them. – Andrew Thompson Jul 22 '13 at 20:55
2 Answers
keyPressed
is fired whenever any key press occurs. keyTyped
is fired when a key is pressed that can be converted into a unicode character. If the shift key is down, for example, pressing "a" will tell keyTyped
that you typed a capital A, and keyPressed
will just get the "a" key, without capital or lowercase designations. You cannot call event.getKeyChar()
from keyPressed
, because there is no key char associated with the events. Characters only come from keyTyped
.
The basic idea is that keyTyped
is used to find characters that are typed, and keyPressed
is used for obtain raw key presses.

- 16,609
- 6
- 58
- 83
KeyPressed
happens when the key goes down. KeyTyped
happens when the key goes down and then back up. I'm not sure if "in rapid succession" is a requirement, and if it is, how fast "rapid" is.
Edit: KeyTyped
is actually when a unicode char is sent from the keyboard. USUALLY, the key behavior is that it goes down and then back up in rapid succession.
Taken from: KeyListener, keyPressed versus keyTyped

- 1
- 1

- 4,181
- 2
- 18
- 28