how can I detect arrow keys in java?
I would recommend using:
if (event.getKeyCode() == KeyEvent.VK_UP) {
...
}
repeating with VK_DOWN, VK_LEFT, VK_RIGHT
.
There are seperate codes for the numeric keypad: VK_KP_UP, VK_KP_DOWN, VK_KP_LEFT, VK_KP_RIGHT
if you need them.
See KeyEvent for all of the codes.
KeyEvent.VK_LEFT
, KeyEvent.VK_RIGHT
, etc.
Also, you should use getKeyCode
, not getKeyChar
. getKeyChar
is for keys that actually correspond to characters (letters, numbers, spaces, etc.).
Use
if ( e.getKeyCode() == KeyEvent.VK_LEFT){
//Do something
}
The other keys are:
KeyEvent.VK_UP
KeyEvent.VK_RIGHT
KeyEvent.VK_DOWN