Lanterna 3. How can i get keypad inputs like enter or F1?

1

In lanterna, how can I read keyboard inputs like Enter or F1? I need to be able to listen to the keyboard input at all times to take actions, as well as listen to the enter key to read the content of the textbox.

public static void main(String[] args) throws IOException, InterruptedException {

    SpringApplication.run(LanternaApplication.class, args);

    TelnetTerminalServer server = new TelnetTerminalServer(2000, Charset.forName("utf8"));
    final TelnetTerminal telnetTerminal = server.acceptConnection();
    KeyStroke keyPressed = telnetTerminal.readInput();
    System.out.println("keyPressed: " + keyPressed.getKeyType());

    // Setup screen layers
    Screen screen = new TerminalScreen(telnetTerminal);
    screen.startScreen();

    // Create main panel to hold components
    Panel mainPanel = new Panel();
    mainPanel.setLayoutManager(new GridLayout(1));

    //input
    final TextBox tbox = new TextBox(new TerminalSize(40, 1));
    tbox.withBorder(Borders.singleLine());
    mainPanel.addComponent(tbox);

    //label
    mainPanel.addComponent(new Label("New Label"));

    // Create window to hold the panel
    BasicWindow window = new BasicWindow();
    window.setComponent(mainPanel);

    // Create gui and start gui
    MultiWindowTextGUI gui = new MultiWindowTextGUI(screen, new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.DEFAULT));
    gui.addWindowAndWait(window);

}

I tried an infinite cycle but it is unreachable after the window is waiting. gui.addWindowAndWait (window);

 while(true){
    // Read input
    KeyStroke keyPressed = telnetTerminal.readInput();

    // Check the input for the "tab" key
    if (keyPressed.getKeyType() == KeyType.F1){
        System.out.println("keyPressed: " + keyPressed.getKeyType());
    }
}

Thank you!

    
asked by Arturo González Villamizar 29.04.2016 в 14:33
source

1 answer

1

Looking at the source code of the TextBox class, you have programmed the behavior for certain keys:

          case Enter:
            if(style == Style.SINGLE_LINE) {
                return Result.MOVE_FOCUS_NEXT;
            }
            String newLine = line.substring(caretPosition.getColumn());
            String oldLine = line.substring(0, caretPosition.getColumn());
            if(validated(newLine) && validated(oldLine)) {
                lines.set(caretPosition.getRow(), oldLine);
                lines.add(caretPosition.getRow() + 1, newLine);
                caretPosition = caretPosition.withColumn(0).withRelativeRow(1);
            }
          return Result.HANDLED;

As you can see, here is defining its own behavior based on the keys it supports, that is, all this framework has defined behaviors in its components by some keys.

I recommend that you look at the list of components that it offers and the directory of examples that it has in the project repo Lanterna

In none of the examples uses any listener by which you can analyze the key press event and from that point decide what you do, in the style Swing

If this is not good for you, you can extend this class and overload the method in question to try the keys you want, although I do not see it necessary.

    
answered by 29.04.2016 / 16:11
source