This question already has an answer here:
- How do I check if the user is pressing a key? 3 answers
I am a beginner in Java an I have been looking on how to detect if the user pressed a key (such a the arrow keys). Apparently there are a lot of ways to do such a thing, and I found that this method should work for me:
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch( keyCode ) {
case KeyEvent.VK_UP:
// handle up
break;
case KeyEvent.VK_DOWN:
// handle down
break;
case KeyEvent.VK_LEFT:
// handle left
break;
case KeyEvent.VK_RIGHT :
// handle right
break;
}
}
The problem is that I have no idea what a KeyEvent is.
Can anyone tell me what to put in the parentheses when I call the method and show me an example please?
PS: don't send me to an other site, I probably have already looked at it and they just confuse me more...