如何捕获 onkeydown 事件上的退格键
我有一个由 onkeydown
触发的函数文本框的 事件。如何判断用户是否按下了退格键或德尔键?
I have a function that is triggered by the onkeydown
event of a textbox. How can I tell if the user has hit either the backspace key or the del key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
Try this:
event.key ===“Backspace”或“Delete”
更新且更简洁:使用
event.key
。不再有任意数字代码!Mozilla 文档
支持的浏览器
event.key === "Backspace" or "Delete"
More recent and much cleaner: use
event.key
. No more arbitrary number codes!Mozilla Docs
Supported Browsers
只要您同意不支持旧版浏览器,就可以使用
.key
或 的.code
属性键盘事件
。截至 2023 年 7 月,超过 97% 的用户浏览器支持。使用它们的代码应如下所示像这样的事情:如果由于某种原因您需要支持古老的浏览器,则执行此操作的代码应该使用已弃用的
.keyCode
属性(尽管请参阅下面关于为什么这个旧界面较差的讨论):旧版
更改的一个好处。 keyCode
到较新的.key
和.code
具有更强大且程序员友好的非 ASCII 键处理 - 请参阅 列出所有可能的键值的规范,它们是人类可读的字符串,例如 < code>“Backspace” 和“Delete”
并包含从日语键盘特有的修饰键到晦涩的媒体键等所有内容的值。另一个好处是,新界面区分了修改按键的含义和按下的物理按键。事实证明,这与这个问题高度相关!在 Mac 小键盘上,没有 Delete 键,只有一个退格键。但是,按 Fn+Backspace 相当于在普通键盘上按 Delete - 也就是说,它会删除之后的字符 em> 文本光标而不是其前面的光标。根据您的用例,在代码中您可能需要处理 Backspace 按下,同时按住 Fn 作为 Backspace 或 Delete。这就是为什么新界面允许您选择要检查的内容。
.key
属性为您提供按键的含义,因此 Fn+Backspace 将生成字符串“删除”
。.code
属性为您提供物理按键,因此 Fn+Backspace 仍会生成字符串"Backspace"
。As long as you're okay not supporting ancient browsers, use the
.key
or.code
attributes of theKeyboardEvent
. These are supported in over 97% of users' browsers as of July 2023. Code to use them should look something like this:If for some reason you need to support ancient browsers, code to do this should instead use the deprecated
.keyCode
attribute (though see discussion below of why this older interface is inferior):One benefit of the change from the legacy
.keyCode
to the newer.key
and.code
is having more powerful and programmer-friendly handling of non-ASCII keys - see the specification that lists all the possible key values, which are human-readable strings like"Backspace"
and"Delete"
and include values for everything from modifier keys specific to Japanese keyboards to obscure media keys. Another benefit is that the new interface distinguishes between the meaning of a modified keypress and the physical key that was pressed.This is, it turns out, highly relevant to this question! On small Mac keyboards, there is no Delete key, only a Backspace key. However, pressing Fn+Backspace is equivalent to pressing Delete on a normal keyboard - that is, it deletes the character after the text cursor instead of the one before it. Depending upon your use case, in code you might want to handle a press of Backspace with Fn held down as either Backspace or Delete. That's why the new interface lets you choose which to check for.
The
.key
attribute gives you the meaning of the keypress, so Fn+Backspace will yield the string"Delete"
. The.code
attribute gives you the physical key, so Fn+Backspace will still yield the string"Backspace"
.在您的函数中检查键码 8(退格键)或 46(删除键)
键码信息< br>
键码列表
In your function check for the keycode 8 (backspace) or 46 (delete)
Keycode information
Keycode list
不确定它是否在 Firefox 之外工作:
如果不能,请将
event.DOM_VK_BACK_SPACE
替换为8
,将event.DOM_VK_DELETE
替换为46
code> 或将它们定义为常量(为了更好的可读性)not sure if it works outside of firefox:
if not, replace
event.DOM_VK_BACK_SPACE
with8
andevent.DOM_VK_DELETE
with46
or define them as constant (for better readability)