How to read a single char from the console in Java (as the user types it)?
Solution 1
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.
Solution 2
You need to knock your console into raw mode. There is no built-in platform-independent way of getting there. jCurses might be interesting, though.
On a Unix system, this might work:
String[] cmd = {"/bin/sh", "-c", "stty raw </dev/tty"};
Runtime.getRuntime().exec(cmd).waitFor();
Solution 3
I have written a Java class RawConsoleInput that uses JNA to call operating system functions of Windows and Unix/Linux.
- On Windows it uses
_kbhit()
and_getwch()
from msvcrt.dll. - On Unix it uses
tcsetattr()
to switch the console to non-canonical mode,System.in.available()
to check whether data is available andSystem.in.read()
to read bytes from the console. ACharsetDecoder
is used to convert bytes to characters.
It supports non-blocking input and mixing raw mode and normal line mode input.
Solution 4
There is no portable way to read raw characters from a Java console.
Some platform-dependent workarounds have been presented above. But to be really portable, you'd have to abandon console mode and use a windowing mode, e.g. AWT or Swing.
Solution 5
Use jline3:
Example:
Terminal terminal = TerminalBuilder.builder()
.jna(true)
.system(true)
.build();
// raw mode means we get keypresses rather than line buffered input
terminal.enterRawMode();
reader = terminal .reader();
...
int read = reader.read();
....
reader.close();
terminal.close();
Oleg Safarov
I'm a Mexican Java programmer who likes surf, wine and digital arts. Baja, Mexico
Updated on October 08, 2021Comments
-
Oleg Safarov over 2 years
Is there an easy way to read a single char from the console as the user is typing it in Java? Is it possible? I've tried with these methods but they all wait for the user to press enter key:
char tmp = (char) System.in.read(); char tmp = (char) new InputStreamReader(System.in).read (); char tmp = (char) System.console().reader().read(); // Java 6
I'm starting to think that System.in is not aware of the user input until enter is pressed.
-
Ryan Fernandes almost 15 yearsJCurses is not very portable either.... From the JCurses README: "JCurses consists of two parts: the plattform independent part, and plattform dependent part, that consists of a native shared library making primitive input and output operations available to the first part."
-
MrSmith42 over 8 yearsWorked fine for me under Linux
-
Nic almost 8 yearsHow heavily has this been tested/stress-tested?
-
Christian d'Heureuse almost 8 years@QPaysTaxes Stress-testing is difficult for console input. I think, in this case it would be more important to test it in various environments (different Windows/Linux versions, 64/32 bit, Linux via SSH, Telnet, serial port or desktop console, etc.). So far I only use it in my private test tools. But the source code is relatively small, compared to other solutions (like JLine2 which uses Jansi). So there is not much that can go wrong. I wrote it, because JLine2 does not support single character input without blocking.
-
Nic almost 8 yearsThat's what I meant by stress-tested -- it's probably the wrong word; my bad. Anyway, nice! I've stolen^H^H^H^H^H^Hused it in a project of mine for school and it helped a bunch.
-
Igor over 7 yearsHey - this class looks great. However: I cannot get it to work.. how am I supposed to use it? I have encountered System.in blocking until I press CTRL+D (on Linux) and now I read about console modes and the likes. I think your RawConsoleInput is what I am looking for - but how do I use it?
-
Christian d'Heureuse over 7 years@Igor Just call RawConsoleInput.read(boolean) to read a keyboard character. It's documented in the source code (RawConsoleInput.java).
-
Igor over 7 years@Christiand'Heureuse: thanks! I had posted the comment, but after a while I found out how it works. Thank you! It works quite well in a linux terminal (have not tried it on Windows (yet)).
-
Kelvin about 7 yearsWorked on Mac as well. You probably want to mention that
stty cooked </dev/tty
should be run when the program needs to revert to buffered mode, and definitely before the program exits. -
Antoniossss over 6 years@RyanFernandes sounds quite portable to me - single tool that can be run on multiple systems (using different dependencies)
-
RawToast about 6 yearsI found that RawConsoleInput based solutions didn't work on MacOS High Sierra; however, this works perfectly.
-
lepe almost 6 yearsjline has practically all you need to create an interactive console/terminal system. It works great in Linux. For a more complete example look at: github.com/jline/jline3/blob/master/builtins/src/test/java/org/… . It has autocomplete, history, password mask, etc.
-
trindflo about 5 yearsVery useful. You might want to add a simple example, e.g.
public static void main(String[] args) { RawConsoleInput GC = new RawConsoleInput(); int CharRead = 0; for (;;) { try { CharRead = GC.read(true); } catch (IOException ex) { Logger.getLogger(RawConsoleInput.class.getName()).log(Level.SEVERE, null, ex); } if (CharRead == -1 || CharRead == 27 || CharRead == 3 || CharRead == 4) // ^c, ^d, or Esc) break; switch (CharRead) { case } } }
-
mike rodent about 5 yearsNice... I got this working in a Windows command prompt and assume it works fine in Linux. Unfortunately I can't get it to work in a Windows Cygwin (BASH) terminal, even if I switch
isWindows
tofalse
. Specifically it gives this error: Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'c': Native library (win32-x86-64/c.dll) not found in resource path (.;jna-4.1.0.jar) at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:271) ... at consolereader.RawConsoleInput.main(RawConsoleInput.java:64) -
M.E. almost 4 yearsAdding the source code to an existing project results in the following error in Netbeans:
package com.sun.jna does not exist
, does this have any external dependencies? Openjdk8 -
WaterGenie over 3 years@M.E. The JNA jar will have to be added in addition to RawConsoleInput.java. Also JNA deprecated
Pointer.SIZE
in version 5 so we also need to replace that withNative.POINTER_SIZE
on line 170. -
Christian d'Heureuse over 3 years@Thirdwater Thanks, I have updated the source code for the current JNA version 5.6.0.