How to read a single char from the console in Java (as the user types it)?

What you want to do is put the console into “raw” mode (line editing bypassed and no enter key required) as opposed to “cooked” mode (line editing with enter key required.) On UNIX systems, the ‘stty’ command can change modes.

Now, with respect to Java… see Non blocking console input in Python and Java. Excerpt:

If your program must be console based,
you have to switch your terminal out
of line mode into character mode, and
remember to restore it before your
program quits. There is no portable
way to do this across operating
systems.

One of the suggestions is to use JNI. Again, that’s not very portable. Another suggestion at the end of the thread, and in common with the post above, is to look at using jCurses.

Leave a Comment