0

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.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by jiraiya
0

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.