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

后端 未结 5 1957
深忆病人
深忆病人 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:26

    Use jline3:

    Example:

    Terminal terminal = TerminalBuilder.builder()
        .jna(true)
        .system(true)
        .build();
    
    // raw mode means we get keypresses rather than line buffered input
    terminal.enterRawMode();
    reader = terminal .reader();
    ...
    int read = reader.read();
    ....
    reader.close();
    terminal.close();
    

提交回复
热议问题