attention if you want to handle any keys that they registered by OS (for example: Alt+Tab ) you CAN NOT do this by Jquery.
您需要将事件分配给未注册的键才能使用 Jquery 触发事件。
你可以尝试一些像blow这样的代码来轻松处理你想要的东西
var keys = {};
$(document).keydown(function (e) {
keys[e.which] = true;
});
$(document).keyup(function (e) {
delete keys[e.which];
});
if( (keys[91] && keys[68]) || (keys[18] && keys[9]) ) /*windows+d OR alt+tab*/
{ /* your code */}
or
use jwerty http://keithcirkel.co.uk/jwerty/lib 来做到这一点。示例代码:
jwerty.key('ctrl+shift+P', function () {
// your code
});
并支持组合:
jwerty.key('⌃+⇧+P/⌘+⇧+P', function () {
// your code
});
and There's a simple javaScript library
Mousetrap http://craig.is/killing/mice for handling keyboard shortcuts. Take a look at the example:
Mousetrap.bind('h', function() {
// your code
});
OR
它还支持组合:
Mousetrap.bind(['ctrl+h', 'ctrl+l'], function(e) {
// your code
}
我希望它对你有用。