Press a key without pressing the enter key in the console

I have a console program written in Java that should respond with one keystroke, but the user does not press the enter key.

I'm doing pong .. so I need the up and down keys to move the beat game.

Alternative approaches are welcome! (besides creating a graphical interface)

- Change:

I will run my program only on UNIX systems (OSX and Linux), so I think I can put the terminal in raw mode with this: stty raw

When I enter it into the console before starting the program, it works! But I need Java to do this automatically, so I tried this:

 Runtime.getRuntime().exec("stty raw"); 

and it does nothing ... perhaps because the JVM just starts it as a separate process, and not as a process inside this terminal.

+6
java console
source share
1 answer

I think you cannot without native code and JNI. Take a look at the Java Curses library: http://sourceforge.net/projects/javacurses/

+5
source share

All Articles