Javascript 命令键的 jQuery 键码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3834175/
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-08-23 06:19:20  来源:igfitidea点击:

jQuery key code for command key

javascriptjquerycommandkeydownonkeyup

提问by Craig

I have read jQuery Event Keypress: Which key was pressed?and How can i check if key is pressed during click event with jquery?

我已阅读jQuery 事件按键:按下哪个键?以及如何使用 jquery 检查在单击事件期间是否按下了键?

However my question is if you can get the same key event for all browsers? Currently I know that Firefox gives the commandbutton (Mac) the code 224 while Chrome and Safari give it the value 91. Is the best approach to simply check what browser the user is using and base the key pressed on that or is there a way so that I can get 1 key code across all browsers? Note I am getting the value with the:

但是我的问题是您是否可以为所有浏览器获得相同的关键事件?目前我知道 Firefox 为command按钮 (Mac) 提供代码 224,而 Chrome 和 Safari 为其提供值 91。是简单地检查用户正在使用的浏览器并将按键基于该浏览器的最佳方法,还是有办法我可以在所有浏览器中获得 1 个关键代码?注意我得到的价值是:

var code = (evt.keyCode ? evt.keyCode : evt.which);

I would love to not use a plugin if possible just because I only need to know about the command/ctrl(windows system) key pressed.

如果可能的话,我希望不使用插件,因为我只需要知道按下的command/ ctrl(Windows 系统)键。

回答by BrunoLM

jQuery already handles that. To check if control was pressed you should use:

jQuery 已经处理了。要检查是否按下了控件,您应该使用:

$(window).keydown(function (e){
    if (e.ctrlKey) alert("control");
});

The list of modifier keys:

修饰键列表:

e.ctrlKey
e.altKey
e.shiftKey

And as fudgey suggested:

正如软糖所建议的那样:

e.metaKey

Might work on MAC. Some other ways here as well.

可能适用于 MAC。这里还有一些其他方式

回答by Tim Down

If you're not interested in the the exact moment the Commandkey is pressed, just whether it is pressed when another key is pressed, then you can use the metaKeyproperty of the event.

如果您对Command按下键的确切时刻不感兴趣,只对按下另一个键时是否按下该键感兴趣,那么您可以使用metaKey事件的属性。

Otherwise, for keydownand keyupevents, the property you need is keyCodein all browsers. Unfortunately, the Commandkey does not have the same key code in all browsers, and the left and right Commands have different values in WebKit (91 and 93 respectively). I can't see an easy way of detecting these keys without some kind of browser sniff, but there may be one. The whichproperty definitely won't help you.

否则,对于keydownkeyup事件,您需要的属性keyCode在所有浏览器中。不幸的是,Commandkey 在所有浏览器中的键码并不相同,并且Command在 WebKit 中left 和 right的值不同(分别为 91 和 93)。如果没有某种浏览器嗅探,我看不到检测这些键的简单方法,但可能有一个。该which物业肯定不会帮你的。

For more information, http://unixpapa.com/js/key.htmlhas comprehensive coverage of key event handling in all the major browsers.

有关更多信息,http://unixpapa.com/js/key.html全面介绍了所有主要浏览器中的关键事件处理。

回答by Flimm

Here you go, try this live in this runnable code snippet:

给你,在这个可运行的代码片段中尝试这个:

$(window).on('keypress', function(event) {
  $("body").append($("<p>").text(
       "ctrlKey " + event.ctrlKey
    + "; altKey " + event.altKey
    + "; metaKey " + event.metaKey
    + "; shiftKey " + event.shiftKey
    + "; isCommandPressed " + isCommandPressed(event)
  ));
  
});

function isCommandPressed(event) {
  return event.metaKey && ! event.ctrlKey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>Focus on this select control and try pressing keys:</p>

<p><select><option>Test</option></select></p>