In Java, what is the best way to capture a keystroke?

前端 未结 3 1139
生来不讨喜
生来不讨喜 2020-12-21 05:13

I\'m having trouble capturing the keystroke in my Java command-line application. Using System.in.read() I don\'t seem to get anything when hitting the tab key. W

3条回答
  •  清歌不尽
    2020-12-21 05:57

    Data from an InputStream is only made available when the user has pressed enter. I think you'll find that if you press enter after tab, the tab will show up.

    In c applications, there is something similar: a shell sets the input mode to raw from cooked, since it needs the raw keystrokes. Readline is the normal library used for this in Linux. I'm not sure what's common on Windows. As far as I can think, something using JNI is the only option here.

提交回复
热议问题