Java KeyListener: KeyTyped Backspace, Esc as input

荒凉一梦 提交于 2019-11-29 02:55:05

问题


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


回答1:


Assuming you have attached the KeyListener properly and have implemented the methods required for that KeyListener, to detect specific key-presses simply add the following code:

public void keyReleased(KeyEvent ke) 
{
    if(ke.getKeyCode() == KeyEvent.VK_BACK_SPACE)
    {  
        //code to execute if backspace is pressed
    }

    if(ke.getKeyCode() == KeyEvent.VK_ESCAPE)
    {
        //code to execute if escape is pressed
    }
}

The KeyEvent class javadocs can be found at the following link: KeyEvent javadocs.
There you can find a list of all of the Java virtual keycodes used to detect keyboard input when implementing Java KeyListeners and KeyEvents. More information about KeyListeners can be found here: How to Write a Key Listener. To use the keyTyped method as asked, see gangqinlaohu's answer.




回答2:


http://www.fileformat.info/info/unicode/char/8/index.htm

When arg0.getKeyChar() is cast to an int: (int)arg0.getKeyChar(), The backspace key comes up with the value 8, and the Esc key comes up with the value 27.




回答3:


I understand this is a very old subject, but I wanted to add an answer that I found helped me greatly with the same issue.

I'm making a chat input for a Java program, and its better to use KeyTyped as opposed to pressed and released (as it excluded the need to filter most characters). Anyways I wanted to make backspace delete characters, but the .getKeyCode() always returned 0 as per the documentation. However, you can use .getKeyChar() to return the character (probably will appear as a square box), and the escape character '\b' to do a comparison.

if(tmp.getKeyChar().equals('\b')) { ... }



回答4:


Only for java 7 / jdk1.7 and above: In case you are checking against event.getKeyCode(), try the event.getExtendedKeyCode() method. In case of the back-space it will return the proper code (in this case, 8) instead of a plain 0. That way there is no need to check against \b:

if (e.getExtendedKeyCode() == KeyEvent.VK_BACK_SPACE) {
    System.out.println("Backspace pressed");
}



回答5:


You can also use e.getExtendedKeyCode() in keyTyped and it returns non-zero values that appear correct.




回答6:


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 :)




回答7:


Check below code, I hope it will work for you.

@FXML
public void keyPresses(KeyEvent ke) 
{
    if (ke.getCode().equals(KeyCode.BACK_SPACE)) {
        System.out.println("It Works Backspace pressed..!");
    }
}


来源:https://stackoverflow.com/questions/15693904/java-keylistener-keytyped-backspace-esc-as-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!