简体   繁体   English

java 灯笼无法从终端读取输入

[英]java lanterna can not read input from terminal

I have problems using the readInput() method from the lanterna package. My codefraction我在使用 lanterna package 的 readInput() 方法时遇到问题。我的 codefraction

    Terminal terminal = TerminalFacade.createSwingTerminal();
    terminal.enterPrivateMode();
    Key key = terminal.readInput();
    if(key.getKind()==Key.Kind.Escape){
    terminal.moveCursor(6, 6);
    terminal.putCharacter('X');

doesn't allow me to do any input in the terminal and therefore creates a nullpointerexception when checking for key.getKind.不允许我在终端中进行任何输入,因此在检查 key.getKind 时会创建空指针异常。 Does anybody have an idea why this happens?有谁知道为什么会这样?

The readInput method is non-blocking. readInput方法是非阻塞的。 It means that it will not hang until a input is found (as ie Scanner does). 这意味着它只有在找到输入后才会挂起(例如Scanner会挂起)。 So you will need your own "blocking method" which waits for an input: 因此,您将需要自己的“阻塞方法”来等待输入:

Key key = terminal.readInput();
while(key == null) {
    Thread.sleep(5); //whatever low value
    key = terminal.readInput();
}
// here key will not be null, so no NullPointerException

I would like to add an updated answer here.我想在这里添加更新的答案。 As of lanterna 3.1.1, the readInput() method is now blocking.从 lanterna 3.1.1 开始,readInput() 方法现在处于阻塞状态。 This means that it will hang until a user inputs.这意味着它将挂起直到用户输入。

You should use pollInput() instead, as this is the new non-blocking method.您应该改用 pollInput(),因为这是新的非阻塞方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM