如何获得键盘输入,如 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());
}
}
谢谢!