3

I'm writing a game based on terminal and I want capture keyboard event, such as w/a/s/d as direction of Up/Left/Below/Right, rather then readLine a input stream with a Enter key.

Besides, I have read swing package, it has some KeyListener interface but it seems must bind with swing UI.

Does there have pure keyboard event without other technology binding in java world?

LoranceChen
  • 2,453
  • 2
  • 22
  • 48
  • this might help you https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html . other solution might be http://stackoverflow.com/questions/11673782/using-java-how-to-detect-keypresses-without-using-gui-components – XtremeBaumer Jan 30 '17 at 14:28
  • @XtremeBaumer, it bind with swing UI, I need a terminal friendly method. – LoranceChen Jan 30 '17 at 14:30
  • then maybe this http://stackoverflow.com/questions/30564519/java-read-key-from-console-without-press-enter-key ? there are some solutions if you look at suggested answers – XtremeBaumer Jan 30 '17 at 14:31
  • 1
    what you want to look into is "raw" mode. The terminal by default does not give you any input unless you hit enter. You can adjust the configuration with the stty utility which you can call from java/scala. Here is an example of doing that https://github.com/jsuereth/streamerz/blob/1edd22e5d56a28655302ada2c6655232a4d63686/ansi/src/main/scala/com/jsuereth/ansi/Stty.scala. – lucidd Jan 30 '17 at 15:37
  • @lucidd, it works with `new BufferedReader(new InputStreamReader(System.in))` and yours `Stty.bufferByCharacter()`. – LoranceChen Jan 31 '17 at 06:17

0 Answers0