我读过了jQuery 事件按键:按下了哪个键? https://stackoverflow.com/questions/302122/jquery-event-keypress-which-key-was-pressed and 如何使用 jquery 检查在单击事件期间是否按下了按键? https://stackoverflow.com/questions/2445613/how-can-i-check-if-key-is-pressed-during-click-event-with-jquery
However my question is if you can get the same key event for all browsers? Currently I know that Firefox gives the command button (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:
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.
jQuery 已经处理了这个问题。要检查控件是否被按下,您应该使用:
$(window).keydown(function (e){
if (e.ctrlKey) alert("control");
});
修饰键列表:
e.ctrlKey
e.altKey
e.shiftKey
正如软糖建议的那样:
e.metaKey
可能适用于 MAC。这里还有一些其他方法 http://answers.google.com/answers/threadview/id/16409.html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)