gpt4 book ai didi

java - Lanterna 3. 如何获得 Enter 或 F1 等键盘输入?

转载 作者:行者123 更新时间:2023-12-01 09:59:23 25 4
gpt4 key购买 nike

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

谢谢!

最佳答案

您可能想要实现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}

关于java - Lanterna 3. 如何获得 Enter 或 F1 等键盘输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36938655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com