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键代码


- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01