JavaScript 监听器,“按键”没有检测到退格键?
我正在使用 keypress
监听器,例如......
addEventListener("keypress", function(event){
}
但是,这似乎没有检测到删除文本的退格键......
是否有不同的监听器可以用来检测这个?
I'm using a keypress
listener eg..
addEventListener("keypress", function(event){
}
However, this doesn't seem to detect a backspace which erases text...
Is there a different listener I can use to detect this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
KeyPress 事件仅针对字符(可打印)键调用,KeyDown 事件针对所有不可打印键引发,例如 Control、Shift、Alt、< kbd>BackSpace 等
更新:
参考。
KeyPress event is invoked only for character (printable) keys, KeyDown event is raised for all including nonprintable such as Control, Shift, Alt, BackSpace, etc.
UPDATE:
Reference.
尝试使用
keydown
而不是keypress
。键盘事件按以下顺序发生:
keydown
、keyup
、keypress
退格键的问题可能是,浏览器将导航回
>keyup
,因此您的页面将看不到keypress
事件。Try
keydown
instead ofkeypress
.The keyboard events occur in this order:
keydown
,keyup
,keypress
The problem with backspace probably is, that the browser will navigate back on
keyup
and thus your page will not see thekeypress
event.不同浏览器的
keypress
事件可能有所不同。我创建了一个 Jsfiddle 来比较 Chrome 和 Firefox 上的键盘事件(使用 JQuery 快捷键)。根据您使用的浏览器,是否会触发
keypress
事件 - 在 Firefox 上,退格键将触发keydown/keypress/keyup
,但仅触发keydown/keyup
在 Chrome 上。触发单键单击事件
在 Chrome 上
keypress
被触发)时,keydown/keyup
如果没有键盘输入(测试使用alt、shift、退格键、箭头键)keydown
仅适用于选项卡?在 Firefox 上
keydown/keypress/keyup
当浏览器注册键盘输入时,也用于退格键、箭头键、Tab 键(所以这里keypress
即使没有输入也会触发)keydown/keyup
用于 alt、shift这应该不足为奇,因为根据https://api.jquery.com/keypress/:
W3C 不推荐使用按键事件类型 (http://www .w3.org/TR/DOM-Level-3-Events/#event-type-keypress)
最后,要回答您的问题,您应该使用
keyup
或keydown
来检测 Firefox 和 Chrome 中的退格键。在这里尝试一下:
The
keypress
event might be different across browsers.I created a Jsfiddle to compare keyboard events (using the JQuery shortcuts) on Chrome and Firefox. Depending on the browser you're using a
keypress
event will be triggered or not -- backspace will triggerkeydown/keypress/keyup
on Firefox but onlykeydown/keyup
on Chrome.Single keyclick events triggered
on Chrome
keydown/keypress/keyup
when browser registers a keyboard input (keypress
is fired)keydown/keyup
if no keyboard input (tested with alt, shift, backspace, arrow keys)keydown
only for tab?on Firefox
keydown/keypress/keyup
when browser registers a keyboard input but also for backspace, arrow keys, tab (so herekeypress
is fired even with no input)keydown/keyup
for alt, shiftThis shouldn't be surprising because according to https://api.jquery.com/keypress/:
The use of the keypress event type is deprecated by W3C (http://www.w3.org/TR/DOM-Level-3-Events/#event-type-keypress)
Finally, to answer your question, you should use
keyup
orkeydown
to detect a backspace across Firefox and Chrome.Try it out on here:
我写的东西,以防有人遇到人们在认为自己处于表单字段中时按退格键的问题
Something I wrote up incase anyone comes across an issue with people hitting backspace while thinking they are in a form field
event.key === "Backspace"
更新且更简洁:使用
event.key
。不再有任意数字代码!Mozilla 文档
支持的浏览器
event.key === "Backspace"
More recent and much cleaner: use
event.key
. No more arbitrary number codes!Mozilla Docs
Supported Browsers
我的数字控制:
试试
BackSpace键代码是8
My numeric control:
try it
BackSpace key code is 8
请改用 keyup / keydown / beforeinput 事件之一。
基于此参考,按键已被弃用并且不更长推荐。
如果您使用“beforeinput”,请注意它的 浏览器兼容性。 “beforeinput”和其他两个之间的区别在于,“beforeinput”在输入值即将更改时被触发,因此对于无法更改输入值的字符,它不会被触发(例如shift,ctr,alt)。
我遇到了同样的问题,通过使用 keyup 解决了。
Use one of keyup / keydown / beforeinput events instead.
based on this reference, keypress is deprecated and no longer recommended.
if you use "beforeinput" be careful about it's Browser compatibility. the difference between "beforeinput" and the other two is that "beforeinput" is fired when input value is about to changed, so with characters that can't change the input value, it is not fired (e.g shift, ctr ,alt).
I had the same problem and by using keyup it was solved.
我正在尝试
keydown
并且 Backspace 没有被捕获。切换到keyup
对我有用。作为参考,以防有人使用
.on
来动态生成内容。I was trying
keydown
and Backspace was not being captured. Switching tokeyup
worked for me.For reference, in case someone is using with
.on
for dynamically generated content.