I do not want to use something like the Scanner class or the BufferedReader class,that's not my purpose. I want to know if the user pressed the UP key, the DOWN key, the LEFT key, or the RIGHT key, what should I use?
Asked
Active
Viewed 1,076 times
-2
-
1Take a look at [this question](https://stackoverflow.com/questions/18037576/how-do-i-check-if-the-user-is-pressing-a-key) – Alex Sveshnikov Nov 10 '21 at 07:03
-
1(1-) This question is not clear. What type of input are you asking about? Are you trying to get input in a GUI? Are you trying to get input from the command line? – camickr Nov 10 '21 at 14:37
1 Answers
0
UP DOWN LEFT and RIGHT as every character in the keyboard have a ASCII code, so you can actually check it out. Normally in Java, instead of checking if a key has been pressed you could use KeyEvents:
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_RIGHT: System.out.println("The right arrow key is
pressed");
// Other operations
break;
case KeyEvent.VK_LEFT: System.out.println("The left arrow key is
pressed");
// Other operations
break;
case KeyEvent.VK_UP: System.out.println("The up arrow key is
pressed");
// Other operations
break;
case KeyEvent.VK_DOWN: System.out.println("The down arrow key is
pressed");
// Other operations
break;
}
}
You have more information in the documentation

Luisa Sanchez
- 113
- 3