1

I am trying capture character codes using keyUp in jQuery. I'm trying to capture both when the = sign (not on the numeric keyboard) and the + sign (also not on the numeric keyboard) are pressed. The thing is, the character key for them is both. If you press it normally, you get =. If you shift+press it, then you get +. My code goes like this:

(document).keyup(function(evt) {
    keyPressed = evt.keyCode ? evt.keyCode : evt.charCode;

    switch (keyPressed) {
        case 187: // perfectly captures the equal key pressed
        case ???: // what should I put here
 // ........

So how do I capture (possibly in this case statement) when they key PLUS shift is being pressed? Is there some elegant way to do it?

daremkd
  • 8,244
  • 6
  • 40
  • 66
  • try this to capture more than one key pressed together. http://stackoverflow.com/questions/4954403/can-jquery-keypress-detect-more-than-one-key-at-the-same-time – Pardeep Dhingra Dec 04 '15 at 13:21

4 Answers4

2

evt has another property of interest to you - shiftKey, which is a boolean indicating that the shift key was pressed. Therefore you can do this:

$(document).keyup(function(evt) {
    keyPressed = evt.keyCode ? evt.keyCode : evt.charCode;
    if(keyPressed == 187){
       if(evt.shiftKey){
           // is + as shift was also pressed
       }
       else{
           // is =
       }
    }
});
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • But you need to handle `AltGr` too or this can give false positive for '=' – A. Wolff Dec 04 '15 at 13:39
  • @A.Wolff can you elaborate, I'm not sure I follow. If I hold `AltGr` and press +/= key I get neither - is this perhaps what you mean? – Jamiec Dec 04 '15 at 14:56
  • On [azerty keyboard](http://static.commentcamarche.net/ccm.net/faq/images/0-Vuskup12-azerty-s-.png), pressing AltGr+[187 key] gives '}'. Now using `keyup` event, you'd have to deal with all possible keyboards giving possible different character depending keyCode. That's why you should use instead `keypress` event. In other answer, there is this posted link: http://stackoverflow.com/questions/11030532/keypress-and-keyup-why-is-the-keycode-different/11030750#11030750 – A. Wolff Dec 04 '15 at 15:05
0

Instead you can use the same keycode and check in the case that shiftkey is pressed:

switch (keyPressed) {
    case 187: 
       if(evt.shiftKey){
          console.log('+');
       }else{
          console.log('=');
       }
    break;
}
Jai
  • 74,255
  • 12
  • 74
  • 103
0

There's a handler for the keyup event called shiftKey:

$(document).keyup(function(evt) {
    keyPressed = evt.keyCode ? evt.keyCode : evt.which;

    switch (keyPressed) {
        case 187:
            if (evt.shiftKey)
                alert("+");
            else
                alert("=");
            break;
    }
});

See Fiddle


Or, depending on what you want to do, you could also try using keypress instead of keyup (The keycode's are different). Take a look
$(document).keypress(function(evt) {
    keyPressed = evt.keyCode ? evt.keyCode : evt.which;

    switch (keyPressed) {
        case 61:
            alert("=");
            break;
        case 43:
            alert("+")
            break;
    }
});

See Fiddle


Also check - Difference between keyup and keypress
Community
  • 1
  • 1
gioNicol
  • 339
  • 1
  • 10
0

Your best bet is to use keypress event:

$(document).keypress(function (evt) {
  var keyPressed = String.fromCharCode(evt.which);
  console.log(keyPressed)
});

event.which is normalized

A. Wolff
  • 74,033
  • 9
  • 94
  • 155