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 に答える