Javascript:不同浏览器上的不同keyCodes?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3883543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:33:03  来源:igfitidea点击:

Javascript: different keyCodes on different browsers?

javascriptkeycode

提问by Senica Gonzalez

So I've seen some forums posts about different browsers reporting differenct keyCodes, but everyone seems so avoid the "why?".

所以我看到了一些关于不同浏览器报告不同 keyCode 的论坛帖子,但每个人似乎都避免“为什么?”。

I was trying to capture the colon (:) keyCode and realized that Firefox reports back e.keyCode 56. While Chrome reports back 186 (I think that's what it was).

我试图捕获冒号 (:) keyCode 并意识到 Firefox 报告回 e.keyCode 56。而 Chrome 报告回 186(我认为这就是它)。

Is there a univeral way of getting the right keyCode across all browsers?

是否有一种通用的方法可以在所有浏览器中获取正确的 keyCode?

And why are they different if they are the same keys?

如果它们是相同的键,为什么它们不同?

I would be more curious as to whether there is a international way of getting the same key press.

我会更好奇是否有获得相同按键的国际方式。

Thanks.

谢谢。

采纳答案by Plaudit Design

See http://unixpapa.com/js/key.htmlfor an explanation why they have different keys. I do not know of an international way to match keys.

请参阅http://unixpapa.com/js/key.html以了解为什么它们具有不同的密钥。我不知道匹配密钥的国际方式。

回答by Tim Down

It depends whether you're interested in which physical key the user has pressed or which character the user has typed. If it's the character you're after, you can get that reliably in all major browsers (using the keypressevent's whichproperty in most browsers or keyCodein IE <= 8), but only in the keypressevent. If you're after the key, use the keydownor keyupevent and examine the keyCodeproperty, although the exact key-code mappings do differ somewhat between browsers.

这取决于您是否对用户按下的物理键或用户输入的字符感兴趣。如果这是您所追求的角色,您可以在所有主要浏览器中可靠地获得该角色(在大多数浏览器或IE <= 8 中使用keypress事件的which属性keyCode),但仅限于keypress事件。如果您在寻找键,请使用keydownorkeyup事件并检查该keyCode属性,尽管确切的键代码映射在浏览器之间确实有所不同。

An excellent explanation of and reference for all JavaScript key-related events can be found at http://unixpapa.com/js/key.html.

可以在http://unixpapa.com/js/key.html找到对所有 JavaScript 键相关事件的出色解释和参考。

To detect the user typing a colon character reliably in all the major browsers, you could do the following:

要在所有主要浏览器中可靠地检测用户键入冒号字符,您可以执行以下操作:

document.onkeypress = function(e) {
    e = e || window.event;
    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    if (charCode && String.fromCharCode(charCode) == ":") {
        alert("Colon!");
    }
};

回答by kidconcept

This is an old question. The modern way to do this is use event.key. See MDN Key

这是一个老问题。现代方法是使用 event.key。见MDN 密钥

回答by ifroz

I think you should make JavaScript to get the keycode of the ':' character, so the script will know what is it in a certain environment. Similar question had been asked here, in stackoverflow.

我认为你应该让 JavaScript 来获取 ':' 字符的键码,这样脚本在特定环境中就会知道它是什么。在 stackoverflow 中,这里也有人问类似的问题。