Unfortunately this is not possible in a portable way:
http://forums.sun.com/thread.jspa?threadID=5351637&messageID=10526512
On Windows, reading from System.in will block until enter
is pressed, even when you do not use a BufferedReader
. Arrows will cycle through the command history. Try it yourself:
import java.io.*;
public class KeyTest {
public static void main(String[] argv) {
try {
InputStreamReader unbuffered = new InputStreamReader(System.in);
for (int i = 0; i < 10; ++i) {
int x = unbuffered.read();
System.out.println(String.format("%08x", x));
}
} catch (Exception e) {
System.err.println(e);
}
}
}
Same issue using the Console
class (input buffered under Windows, arrow keys intepreted by Windows):
import java.io.*;
public class KeyTest2 {
public static void main(String[] argv) {
try {
Console cons = System.console();
if (cons != null) {
Reader unbuffered = cons.reader();
for (int i = 0; i < 10; ++i ) {
int x = unbuffered.read();
System.out.println(String.format("%08x", x));
}
}
} catch (Exception e) {
System.err.println(e);
}
}
}