Java KeyListener: KeyTyped Backspace, Esc as input

前端 未结 7 1058
耶瑟儿~
耶瑟儿~ 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:01

    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')) { ... }
    

提交回复
热议问题