java语言中退格_java – 如何检测keyTypedEvent中的退格

我正在使用Netbeans的bean表单创建我的GUI.我已经将一个keyTyped事件添加到JTextArea,我想检测键入的键是否是退格键.

我出于其他原因使用keyTyped事件,所以我不能只使用keyPressed事件.

这是生成的代码(以及我的if检查):

private void langArea1KeyTyped(java.awt.event.KeyEvent evt) {

if(evt.getChar()== backspace) //how can I make this check?

}

evt.getKeyCode()始终返回0独立于键入的键.

evt.getKeyChar()只删除打印时打印的最后一个字符,因此我无法使用System.out.print(evt.getKeyChar())来检测其值并检查它.

如何检测键入的键是退格键(或删除键)?

解决方法:

退格和删除具有以下常量:

VK_BACK_SPACE

VK_DELETE

WARNING: Aside from those keys that are defined by the Java language (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_ constants. Sun reserves the right to change these values as needed to accomodate a wider range of keyboards in the future.

除此之外,我建议您使用keyPressed()或keyReleased(),因为keyTyped()仅用于生成字符的键.键退格和删除不会产生字符.

额外:

getKeyChar()适用于KEY_TYPED,如here:所述

The getKeyChar method always returns a valid Unicode character or CHAR_UNDEFINED. Character input is reported by KEY_TYPED events: KEY_PRESSED and KEY_RELEASED events are not necessarily associated with character input.

Therefore, the result of the getKeyChar method is guaranteed to be meaningful only for KEY_TYPED events.

你可以看看这些链接:

标签:java,netbeans,keyboard-events,backspace

来源: https://codeday.me/bug/20191002/1841154.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值