Java KeyListener: KeyTyped Backspace, Esc as input

前端 未结 7 1044
耶瑟儿~
耶瑟儿~ 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(){
        @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 :)

提交回复
热议问题