jQuery key code for command key(命令键的jQuery键代码)
问题描述
我已阅读 jQuery 事件按键:按下了哪个键? 和 我如何检查如果在使用 jquery 的单击事件期间按下键?
但是我的问题是,您是否可以为所有浏览器获取相同的键事件?目前我知道 Firefox 为 command 按钮 (Mac) 提供代码 224,而 Chrome 和 Safari 为其提供值 91. 是简单检查用户正在使用的浏览器并按下按键的最佳方法或者有没有办法让我可以在所有浏览器中获得 1 个关键代码?请注意,我通过以下方式获得价值:
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);
如果可能的话,我宁愿不使用插件,因为我只需要知道按下的 command/ctrl(Windows 系统)键.
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 已经解决了这个问题.要检查是否按下了控件,您应该使用:
jQuery already handles that. To check if control was pressed you should use:
$(window).keydown(function (e){
if (e.ctrlKey) alert("control");
});
修饰键列表:
e.ctrlKey
e.altKey
e.shiftKey
正如 fudgey 建议的那样:
And as fudgey suggested:
e.metaKey
可能适用于 MAC.这里还有其他一些方法.
这篇关于命令键的jQuery键代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:命令键的jQuery键代码


基础教程推荐
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 动态更新多个选择框 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01