I’m trying to detect key combinations with jQuery, I’ve been researching for the last like, few hours trying to find a workable solution. Seems like everything works great with the ctrl key but not the command key on a mac. This code is almost just what I want, but how can I make it work with command?
$.ctrl = function(key, callback, args) {
var isCtrl = false;
$(document).keydown(function(e) {
if(!args) args=[]; // IE barks when args is null
if(e.ctrlKey) isCtrl = true;
if(e.keyCode == key.charCodeAt(0) && isCtrl) {
callback.apply(this, args);
return false;
}
}).keyup(function(e) {
if(e.ctrlKey) isCtrl = false;
});
};
This should work:
Why the keyup event? I could be wrong, but I think modifier keys don’t work on
keyup()but they do onkeydown().