命令键的jQuery键代码 [英] jQuery key code for command key

查看:29
本文介绍了命令键的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键代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆