What am I doing wrong? It seems that the array is not cleared after the function was called.
If you press first ctrl+c and then ctrl+alt+c the second function will not called (only if you press it a second time).
var key = function (keys, fn) {
var arr = [];
$(document).on({
keydown: function (e) {
arr.push(e.which);
if (arr.join(', ') === keys) {
fn(e);
arr = [];
}
},
keyup: function (e) {
arr = [];
}
});
};
// ctrl + c
key('17, 67', function (e) {
alert('ctrl+c');
});
// ctrl + alt + c
key('17, 18, 67', function () {
alert('ctrl+alt+c');
});
Here's a fiddle.
keydownandkeyuphandler for every sequence - that doesn't scale 2. ctrl-alt-N is normally treated the same as alt-ctrl-Nctrland pressing 'c' multiple times, not just once and then lifting all fingers.c+ctrlit does not copy anything ;)