Java KeyListener: KeyTyped Backspace, Esc as input

前端 未结 7 1013
耶瑟儿~
耶瑟儿~ 2020-12-29 10:33

Inside the KeyTyped method, how do I tell if Backspace or Esc is being pressed?

相关标签:
7条回答
  • 2020-12-29 11:08

    Personally, I prefer to use the KeyPressed event for keys other than letters/numbers, as when you're typing the Backspace or Enter keys, nothing is actually being typed per say. Here's what I did that worked (make sure KeyCode and KeyEvent are imported from javafx.scene.input)!

    MyTextArea.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>(){
        @Override
        public void handle(KeyEvent event) {
            if (event.getCode().equals(KeyCode.BACK_SPACE)){
                System.out.println("Success");
            }
        }
    });
    

    Let me know if this worked for you :)

    0 讨论(0)
提交回复
热议问题