简体   繁体   中英

Get key press without pressing enter in 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM