1

如何获得键盘输入,如 enter 或 F1?按下回车键后,我需要获取文本框内容。我需要随时获得 F1 和其他键。

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) }

我尝试过使用无限循环,但在 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());
  }
}

谢谢!

4

1 回答 1

4

您可能想要实现WindowListener,如下所示:

public class KeyStrokeListener implements WindowListener {
    public void onInput(Window basePane, KeyStroke keyStroke, AtomicBoolean deliverEvent) {
        System.out.println("Pressed: "+ keyStroke);
    }

    public void onUnhandledInput(Window basePane, KeyStroke keyStroke, AtomicBoolean hasBeenHandled) {
        // TODO Auto-generated method stub
    }

    public void onResized(Window window, TerminalSize oldSize, TerminalSize newSize) {
        // TODO Auto-generated method stub
    }

    public void onMoved(Window window, TerminalPosition oldPosition, TerminalPosition newPosition) {
        // TODO Auto-generated method stub
    }
}

然后将监听器添加到窗口中:

public static void main(String[] args) throws IOException {
    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);

    KeyStrokeListener listener = new KeyStrokeListener();
    window.addWindowListener(listener);

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

你应该得到的输出:

Pressed: KeyStroke{keyType=Character, character=t, ctrlDown=false, altDown=false, shiftDown=false}
于 2016-08-24T13:00:03.550 回答