How to read a single char from the console in Java (as the user types it)?

后端 未结 5 1956
深忆病人
深忆病人 2020-11-21 05:49

Is there an easy way to read a single char from the console as the user is typing it in Java? Is it possible? I\'ve tried with these methods but they all wait for the user t

5条回答
  •  一向
    一向 (楼主)
    2020-11-21 06:29

    You need to knock your console into raw mode. There is no built-in platform-independent way of getting there. jCurses might be interesting, though.

    On a Unix system, this might work:

    String[] cmd = {"/bin/sh", "-c", "stty raw 

    For example, if you want to take into account the time between keystrokes, here's sample code to get there.

提交回复
热议问题