Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get key press without pressing enter in console

Tags:

java

console

I have a console program written in Java that should respond to single key presses, but the user does not press enter.

I'm making pong.. so need the up and down keys to move the bat thing.

Alternative approaches welcome! (apart from making a GUI instead)

-- Edit:

I'm only going to run my program on UNIX systems (OSX and Linux), so I think I can put the terminal into "raw" mode with this: stty raw

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

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

and it does nothing... probably because the JVM is just running it as a separate process and not as a process within this terminal.

like image 297
Matt Avatar asked Dec 25 '10 20:12

Matt


1 Answers

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

like image 91
MK. Avatar answered Nov 07 '22 03:11

MK.