Is it possible to read a character by keyboard in Java?


I would need to know how to read a character by keyboard in Java .

I know you can read a number, or a phrase, but this time I would like you to read just one character, and that the system displays a message, immediately after pressing that character, and not when I touch Enter .

The following code receives an integer, then I touch Enter , and it is saved.

Scanner teclado = new Scanner(;
int número = teclado.nextInt();

But what I want is to save a character without pressing the key. Is it possible?

asked by ArtEze 27.11.2016 в 23:07

5 answers


No is possible to perform in console mode.

For more information you can see the following link in English :

answered by 17.01.2017 / 16:05

With the class Scanner we can find different functions, such as capturing strings, numbers, etc:

Scanner teclado = new Scanner(;
int número = teclado.nextInt();

The following takes the first character of what you enter:

char c =;

I do not remember at all if in this way it prevents you from capturing more than one character, I hope it is of your use.

answered by 13.01.2017 в 16:45

Yes, it is possible. I leave you here a little program that asks for a character and then shows it on the screen.

public static void main(String[] args) {

    Scanner escaneo = new Scanner(;
    System.out.println("Introduzca un carácter: ");
    char caracter;
    caracter =;



As for showing a message without pressing the intro, it escapes me a little. If I find the form I edit the message and add it. Anyway, I suppose it can be done. With any language you can do anything, even if it is more or less complicated, in the end you take

answered by 01.01.2019 в 01:03

What you are trying to do I suppose is related to the use of keyboard events . KeyAdapter would help you.

It is advisable to make a new class for the event by extending the KeyAdapter class. To not write all the methods of the KeyListener interface, you could use the keyPressed(KeyEvent e) method. more information .

answered by 02.12.2016 в 08:08

Try this:

//Primero importamos la clase scanner
import java.util.Scanner;

public class NewClass {
//definimos el metodo main
public static void main(String[]args)throws Exception{
    //Declaramos el objeto teclado
    Scanner teclado = new Scanner(;
 // declaramos lavariable caracter
char caracter;
//Mensaje al usuario para que introduzca un caracter.
System.out.println("Introducir caracter: ");
caracter =;
answered by 06.01.2019 в 03:13