Detecting and acting on keyboard direction keys in Java

elwynn picture elwynn · Feb 21, 2009 · Viewed 19k times · Source

G'day all,

I have a console project where it is intended the user presses the keyboard direction keys (non-numeric keypad) to move an avatar. I am having difficulty coding to check for the press of these keys. In Pascal it was easy enough to use "readkey" and code, for example, for #80 for the down keypress. However, I am stumped how to implement the same functionality in Java, though I think I understand the use of System.in and BufferedInputStream.

Could anyone help me out? Your thoughts or hints are much appreciated.

Answer

Jason S picture Jason S · Feb 21, 2009

If java.io.console doesn't work for you (I haven't tried that), try JLine. I used it to solve a vaguely similar problem.