Search code examples
javalanterna

java lanterna can not read input from terminal


I have problems using the readInput() method from the lanterna package. My 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. Does anybody have an idea why this happens?


Solution

  • The readInput method is non-blocking. It means that it will not hang until a input is found (as i.e. Scanner does). 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