public class ConsoleNonblocking
extends java.lang.Object
Example of doing non-blocking console input on Unix.
Tested on Linux and OSX.
Based on ideas from Here.
Modifier and Type | Field and Description |
---|---|
private static boolean |
configDone
whether we've configured the TTY yet
|
static int |
ESC
ASCII escape character
|
Constructor and Description |
---|
ConsoleNonblocking() |
Modifier and Type | Method and Description |
---|---|
static int |
getChar()
getChar(int) with no timeout |
static int |
getChar(int timeoutMS)
Like System.in.read() but nonblocking.
|
static void |
main(java.lang.String[] args)
Demo program, reads input until user hits ESC.
|
static java.lang.String |
stty(java.lang.String cfg)
Run stty with the given options.
|
public static final int ESC
private static boolean configDone
public static int getChar(int timeoutMS) throws java.io.IOException, java.lang.InterruptedException
timeoutMS
- max milliseconds to wait, or 0 to wait foreverConfigures terminal for nonblocking input on first call; registers shutdown hook to restore it.
java.io.IOException
java.lang.InterruptedException
public static int getChar() throws java.io.IOException, java.lang.InterruptedException
getChar(int)
with no timeoutjava.io.IOException
java.lang.InterruptedException
public static java.lang.String stty(java.lang.String cfg) throws java.io.IOException, java.lang.InterruptedException
cfg
- the stty optionsjava.io.IOException
java.lang.InterruptedException
public static void main(java.lang.String[] args) throws java.io.IOException, java.lang.InterruptedException
java.io.IOException
java.lang.InterruptedException