tags:

views:

40

answers:

3

how do i check if a combination of keys are pressed with jquery?

lets say i want to fire up an alert only when the up and down arrow keys are pressed at the same time.

right now im just using:

switch (event.which) {
    case 40:
        alert('down');
        break;
    case 38:
        alert('up');
        break;
    case 37:
        alert('left');
        break;
    case 39:
        alert('right');
        break;
}
+4  A: 

Set flags. When one key goes down, if it's a certain keyCode, set your flag myKeyIsDown = true. When it comes up, set the flag back to false. When your second key goes down, if it is of a certain keyCode and your myKeyIsDown flag is true, you've got two keys down.

Jonathan Sampson
smart way of handling it..ill have a look on that
weng
This may work, but you'll also need to watch the "keyup" events to clear the flags. Also, keyboards vary as to their willingness to allow multiple keys to be pressed. They'll work with things like "shift" and "ctrl" of course, but sometimes other keys don't. Finally, auto-repeat may cause interesting problems, because you'll get a stream of keydown/keyup events when you hold down a regular key as if it was a "shift" key.
Pointy
worked like a charm
weng
+2  A: 

You could try using a plugin like this (demo)

Rowan
this is a sexy plugin
Natrium
That is a really awesome plugin, but I'm not sure it'd allow you to bind a single handler only for the combination of "uparrow" and "downarrow". The combinations it understands seem to be the "normal" ones, like "shift" and "alt" and "ctrl" + something.
Pointy
@Pointy, good point.
Natrium
yes .. that is what i found out too...i couldnt do: $('#search').bind('keydown', 'left+right', function() { alert('yes');});. it didnt work
weng
+1  A: 

One suggestion in followup to another answer, you might want to stop the cascade of the event on the second key press, which could be done similar to this: (put your logic in of course)

/* handle special key press */ 
function checkCptKey(e) 
{ 
    var shouldBubble = true; 
    switch (e.keyCode) 
    { 
        // user pressed the Tab                                                                                                                                         
        case 9: 
            { 
                $(".someSelect").toggleClass("classSelectVisible"); 
                shouldBubble = false; 
                break; 
            }; 
            // user pressed the Enter     
        case 13: 
            { 
                $(".someSelect").toggleClass("classSelectVisible"); 
                break; 
            }; 
            // user pressed the ESC 
        case 27: 
            { 
                $(".someSelect").toggleClass("classSelectVisible"); 
                break; 
            }; 
    }; 
    /* this propogates the jQuery event if true */ 
    return shouldBubble; 
}; 

/* user pressed special keys while in Selector */ 
$(".mySelect").keydown(function(e) 
{ 
    return checkCptKey(e); 
}); 
Mark Schultheiss
wow..i want to give you 2 points for this=)
weng