1

I tried to do this code:

$(input).keyup(function (e)
{
    alert(e.shiftKey);
});

But every character that I write shows "false", how can I do that?

user1123379
  • 334
  • 3
  • 5
  • 13
  • I'm not sure quite what you're asking. If you're asking about distinguishing between left and right modifier keys (shift, ctrl, alt etc.) then it's not generally possible. See http://stackoverflow.com/a/8562791/96100 – Tim Down Feb 14 '12 at 12:38
  • I want to do this code: if ( left shift + left alt) { ... } – user1123379 Feb 14 '12 at 12:47
  • 2
    In which event? Are any other keys pressed? As I mentioned, you won't generally be able to distinguish between left and right shift or alt. – Tim Down Feb 14 '12 at 12:53

4 Answers4

3

Sadly this is not possible with the event API — both shift keys return keyCode 16, both ctrl keys return 17 — they're indistinguishable!

Sorry for the bad news :(

Barney
  • 16,181
  • 5
  • 62
  • 76
1

Your problem is using keyup(). By the time the event fires the shift key was released and is not more active. Try using keydown() or keypress().

KillerX
  • 1,436
  • 1
  • 15
  • 23
0

Try

$("input").keydown(function (e)
{
    if(e.shiftKey && e.ctrlKey){ alert("shift + crtl Pressed");}
});

this works fine

Parag Gajjar
  • 700
  • 1
  • 11
  • 25
0

The follow will be triggered while both keys are down:

$(input).keydown(function (e) {
    if (e.shiftKey && e.ctrlKey) {
        // This code would run if both the control and shift are down.
    }
});

However, it will continue to be triggered for as long as they are down, see the jQuery example by holding down both keys in the input box.

I thought jQuery's .keypress() would be able to let you know when they were pressed rather than just held, but it appears that it only gets called on character keys a-z, 0-1, ?!>~#, etc.

Thomas Nadin
  • 1,167
  • 10
  • 22