I would need to know how to read a character by keyboard in Java .
I know that you can read a number, or a phrase, but this time I would like it to read only one character, and the system to show a message, immediately after pressing that character, and not when I touch Intro.
The following code receives an integer, then I hit Intro, and it saves.
Scanner teclado = new Scanner(System.in);
int número = teclado.nextInt();
But what I want is to save a character without pressing the key. It's possible?
With the class
Scanner
we can find different functions, such as capturing strings, numbers, etc:The following takes the first character of whatever you enter:
I do not remember at all if this way prevents you from capturing more than one character, I hope it will be useful to you.
It is not possible to perform in console mode.
For more information you could see the following link in English :
If possible. I leave you here a little program that asks for a character and then shows it on the screen.
As far as displaying a message without pressing enter is a bit beyond me. If I find a way, I edit the message and add it. Anyway, I guess it can be done. With any language you can do anything, even if it is more or less complicated, in the end you get
Try this:
What you are trying to do I assume is related to the use of keyboard events . KeyAdapter would help you.
It is recommended to make a new class for the event by extending the KeyAdapter class. To avoid writing all the methods of the KeyListener interface, you could use the
keyPressed(KeyEvent e)
. more information .