8

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.

Matt
  • 11,157
  • 26
  • 81
  • 110
  • 1
    On your edit -- you could run your program from a shell script, first doing stty raw and then launching java yourapp.jar, this way it will probably work too. – MK. Dec 25 '10 at 23:05

1 Answers1

5

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

MK.
  • 33,605
  • 18
  • 74
  • 111