Reputation: 3150
This key event is not working. The same code is working for,
VK_SPACE
Its not working for control
private void jTextArea1KeyPressed(java.awt.event.KeyEvent evt) {
if ((evt.getKeyChar() == KeyEvent.VK_CONTROL)) {
System.out.println("CONTROL IS PRESSED");
}
}
Upvotes: 1
Views: 253
Reputation: 61548
There is a method on the java.awt.event.KeyEvent
just for your purpose - isControlDown()
Upvotes: 3
Reputation: 60868
Don't use getKeyChar
in combination with those VK_
constants. Use getKeyCode
instead. getKeyChar
is for printable keys only, which result in a character being printed in normal operations. getKeyCode
, on the other hand, is intended to give you the code (i.e. the VK_
constant) of the key pressed, even if there is no associated character (as in the case of Ctrl).
See also this answer.
Upvotes: 3
Reputation: 109813
there no reason to use KeyListener
or KeyBindings
or AWTEventDispatch
use DocumentListener for JTextComponents for event to outside from JTextCompoent
use DocumentFilter for filtering inside Document
Upvotes: 3