If you care about the order and also need to hold a key and tap another (ex: Shift + DEL, DEL, DEL...), without having to lift up on the first key to get the event to fire again... I've modified @BlakePlumm's [fiddle] comment which extended @lu1s' comment on @ParthThakkar's answer.
Also, using jQuery's .on() allows you to listen for the key sequence only on certain elements. Change 'body' to 'input.selector' or whatever else.
var map = [];
var down = [];
$(document).on('keydown','body', function(e) {
if(!map[e.which]){
down.push(e.which);
if(down[0] === 68 && down[1] === 69 && down[2] === 86) {
console.log('D + E + V');
} else if(down[0] === 16 && down[1] === 46) {
console.log('SHIFT + DEL');
}
/* more conditions here */
}
map[e.which] = true;
}).keyup(function(e) {
map[e.which] = false;
/* important for detecting repeat presses of
last key while holding first key(s)
(can be shortened. see fiddle) */
var len = down.length;
while (len--) {
if(down[len] === e.which) down.splice(len,1); //removes only the keyup'd key
}
$('.alert').html('');
});
Additional thoughts:
If you only kinda care about the order - that is, the first keys just need to be down, as long as your main event-firing key is pressed last (stuff like CTRL+SHIFT+TAB, TAB, TAB), add this condition:
else if(down.length>2) {
if($.inArray(68,down)!=-1 && $.inArray(69,down)!=-1 && down[2] === 86) {
$('.alert').html('A hacky D+E+V was pressed');
}
}
fiddle with more glorious options and live demo: - http://jsfiddle.net/kstarr/4ftL1p3k/