gpt4 book ai didi

javascript - 在 JavaScript 中的按键上捕获 KeyCode

转载 作者:行者123 更新时间:2023-11-30 18:01:09 25 4
gpt4 key购买 nike

我目前正在尝试在 JavaScript 中为文本框捕获按键事件中的按键代码。我正在使用以下行来检测键码:

var iKey = e.keyCode || e.charCode || e.which || 0;

但是,箭头键出现为 '、% 等等。

任何人都可以阐明这一点吗?任何帮助将不胜感激。

最佳答案

你应该这样做:

var iKey = e.keycode || e.which || 0;

Quirksmode explains your problem well :

The two properties are keyCode and charCode. Put (too) simply, keyCode says something about the actual keyboard key the user pressed, while charCode gives the ASCII value of the resulting character. These bits of information need not be the same; for instance, a lower case 'a' and an upper case 'A' have the same keyCode, because the user presses the same key, but a different charCode because the resulting character is different.

关于javascript - 在 JavaScript 中的按键上捕获 KeyCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16962264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com