简体   繁体   English

Java KeyListener:KeyTyped Backspace,Esc作为输入

[英]Java KeyListener: KeyTyped Backspace, Esc as input

KeyTyped方法内部,如何判断是否按下了BackspaceEsc

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: 假设您已正确附加KeyListener并已实现该KeyListener所需的方法,要检测特定的按键,只需添加以下代码:

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 . 可以在以下链接中找到KeyEvent类javadoc: 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. 在那里,您可以找到用于在实现Java KeyListeners和KeyEvent时检测键盘输入的所有Java虚拟键码的列表。 More information about KeyListeners can be found here: How to Write a Key Listener . 有关KeyListeners的更多信息,请参见: 如何编写密钥监听器 To use the keyTyped method as asked, see gangqinlaohu's answer . 要使用keyTyped方法,请参阅gangqinlaohu的答案

http://www.fileformat.info/info/unicode/char/8/index.htm 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. arg0.getKeyChar()转换为int: (int)arg0.getKeyChar() ,退格键出现值8,Esc键出现值27。

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). 我正在为Java程序进行聊天输入,最好使用KeyTyped而不是按下和释放(因为它排除了过滤大多数字符的需要)。 Anyways I wanted to make backspace delete characters, but the .getKeyCode() always returned 0 as per the documentation. 无论如何我想制作退格删除字符,但.getKeyCode()总是按照文档返回0。 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. 但是,您可以使用.getKeyChar()返回字符(可能会显示为方框),并使用转义字符'\\ b'进行比较。

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

Only for java 7 / jdk1.7 and above: In case you are checking against event.getKeyCode() , try the event.getExtendedKeyCode() method. 仅适用于java 7 / jdk1.7及更高版本:如果要检查event.getKeyCode() ,请尝试使用event.getExtendedKeyCode()方法。 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 : 在后退空间的情况下,它将返回正确的代码(在这种情况下,8)而不是普通的0.这样就没有必要检查\\b

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

您还可以在keyTyped使用e.getExtendedKeyCode() ,并返回看似正确的非零值。

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. 就个人而言,我更喜欢将KeyPressed事件用于除字母/数字之外的其他键,因为当您键入Backspace或Enter键时,实际上每个人都没有输入任何内容。 Here's what I did that worked (make sure KeyCode and KeyEvent are imported from javafx.scene.input)! 这就是我所做的工作(确保从javafx.scene.input导入KeyCode和KeyEvent)!

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 :) 如果这对您有用,请告诉我:)

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..!");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM