ohmm
Class ConsoleNonblocking

java.lang.Object
  extended by ohmm.ConsoleNonblocking

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.

Author:
Marsette A. Vona

Field Summary
private static boolean configDone
          whether we've configured the TTY yet
static int ESC
          ASCII escape character
 
Constructor Summary
ConsoleNonblocking()
           
 
Method Summary
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ESC

public static final int ESC
ASCII escape character

See Also:
Constant Field Values

configDone

private static boolean configDone
whether we've configured the TTY yet

Constructor Detail

ConsoleNonblocking

public ConsoleNonblocking()
Method Detail

getChar

public static int getChar(int timeoutMS)
                   throws java.io.IOException,
                          java.lang.InterruptedException
Like System.in.read() but nonblocking.

Parameters:
timeoutMS - max milliseconds to wait, or 0 to wait forever
Returns:
the character or -1 on timeout

Configures terminal for nonblocking input on first call; registers shutdown hook to restore it.

Throws:
java.io.IOException
java.lang.InterruptedException

getChar

public static int getChar()
                   throws java.io.IOException,
                          java.lang.InterruptedException
getChar(int) with no timeout

Throws:
java.io.IOException
java.lang.InterruptedException

stty

public static java.lang.String stty(java.lang.String cfg)
                             throws java.io.IOException,
                                    java.lang.InterruptedException
Run stty with the given options.

Parameters:
cfg - the stty options
Returns:
stdout of the stty command
Throws:
java.io.IOException
java.lang.InterruptedException

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException,
                        java.lang.InterruptedException
Demo program, reads input until user hits ESC.

Throws:
java.io.IOException
java.lang.InterruptedException