当用户在 Chrome 上按下键盘时,如何检测“删除”和“.”?
当我按下 .
时,它会触发三个事件:keydown
、keypress
和 keyup
。
keydown
which: 190 == ¾
keyCode: 190 == ¾
keypress
which: 46 == .
keyCode: 46 == .
keyup
which: 190 == ¾
keyCode: 190 == ¾
当我按下delete
时,它会触发两个事件:keydown
和keyup
。
keydown
which: 46 == .
keyCode: 46 == .
keyup
which: 46 == .
keyCode: 46 == .
我想按 .
并能够获取相应的字符(46 == .
)。但在 keydown
和 keyup
上,我得到 190
,即 3/4
。在keypress
上,我得到了正确的值,但是当我按delete
时,不会触发此事件。
当我按 delete
时,我在 keydown
和 keyup
上得到代码 46
。但代码 46
是 .
,而不是 delete
。
如果是按下 .
键或 delete
键,如何创建一个事件来捕获这两个键并区分它们的区别?
要测试的页面: http://jsfiddle.net/Eg9F3/
When I press .
it fires the three events, keydown
, keypress
and keyup
.
keydown
which: 190 == ¾
keyCode: 190 == ¾
keypress
which: 46 == .
keyCode: 46 == .
keyup
which: 190 == ¾
keyCode: 190 == ¾
When I press delete
it fires two events, keydown
and keyup
.
keydown
which: 46 == .
keyCode: 46 == .
keyup
which: 46 == .
keyCode: 46 == .
I want to press .
and be able to get the corresponding character (46 == .
). But on keydown
and keyup
I get 190
which is ¾
. On the keypress
I get the correct value, but this event is not fired when I press delete
.
When I press delete
I get the code 46
on keydown
and keyup
. But the code 46
is a .
and not delete
.
How can I make an event to capture both keys and tell the difference, if it was a .
pressed or delete
key?
Page to test: http://jsfiddle.net/Eg9F3/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为最好的解决方案是映射您想要的键(demo),然后使用
e. which
交叉引用按下的键。有一个关于跨浏览器keyCode
值的 很好的参考,它可以工作在这种情况下,因为 jQuery 标准化了e.which
值。这与 jQuery UI 使用 - 查看文件顶部的键码交叉引用?这也是我在 keycaster 插件中使用的方法。
I think the best solution would be to map the keys you want (demo), then use
e.which
to cross-reference what key was pressed. There is a good reference of cross-browserkeyCode
values, which work in this case because jQuery normalizes thee.which
value.This is similar to the method that jQuery UI uses - see the keycodes cross-reference at the top of the file? And it is also the method I use in the keycaster plugin.
事实上,这很奇怪,但却是逻辑。
函数 String.fromCharCode 正在处理真正的字符代码,而不是 KB 操作(左移、删除...)
为什么不简单地按 keyCode 进行过滤?
In fact it's strange but it is logic.
The function String.fromCharCode is working with real char code, not with KB actions (left, delete...)
Why don't filter by keyCode simply ?
我强制执行与 Firefox 相同的行为,jsFiddle 上的示例
I've forced the same behavior as Firefox, example on jsFiddle
“keypress”处理程序中“which”的值与“keydown”或“keyup”处理程序中“which”的含义不同。其中,它是键盘上按键的键码,而不是字符序数。在“keypress”中,它是字符,但您不会得到
Del
的“keypress”(正如您所注意到的)。因此,“keypress”处理程序(“.”)中的 46 与
Del
的“keydown”和“keyup”事件中的 46 不同。您可能应该使用“keydown”或“keyup”并检查键码。
The value of "which" in a "keypress" handler does not have the same meaning as that of "which" in a "keydown" or "keyup" handler. In those, it's the keycode for the key on the keyboard, and not a character ordinal. In "keypress" it is the character, but you don't get a "keypress" for
Del
(as you've noticed).Thus, that's a different 46 in the "keypress" handler (for ".") than in the "keydown" and "keyup" events for
Del
.You probably should use either "keydown" or "keyup" and check for the keycode.