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.

Recommended Answers

All 2 Replies

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:
http://java.sun.com/docs/books/tutorial/uiswing/events/documentlistener.html

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:
http://java.sun.com/docs/books/tutorial/uiswing/events/documentlistener.html

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.