Hi all,

Does anyone know of a way to scan a character from the command line without the enter key having to be pressed?

For example, the user would type 'a', which is then picked up and scanned by the program and stored, etc.

The only way of scanning input that I know of is by using Scanner, but this requires the enter key to be pressed.

Thanks in advance.

If your program uses the command line, then I believe it will be the shell/consoles job to catch the enter or any other key press and not the actual java program.

There may be a way to do it from the console ( probably using java.lang.Runtime ), but it can easily be done using a GUI.

One way is with a DocumentListener object:

Thanks chaospie!

Ideally I'd like to use a GUI, so that sounds like the perfect suggestion. I'm still quite new to Java and haven't tried implementing a GUI yet, but here goes.

Thanks again!

This question has already been answered. Start a new discussion instead.