5

When I bind the ? and the / key with javascript on qwerty keyboard they have the same keycode (191) but you have to press shift to do a ?.

How can I tell which character was pressed on an azerty keyboard (layout shown below), as they're different keys, both require Shift, and I get the same keycode for them in keyup.:

 $(document).keyup(function(event) {
        if (event.which === 191) {
            action();
        }
    });

Layout

(Original image is "KB France" by Yitscar (English Wikipedia) Michka B (French Wikipedia) Licensed under Creative Commons Attribution-Share Alike 3.0 via Wikimedia Commons - see use in the article linked above.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
agauchy
  • 117
  • 3
  • 10
  • 1
    Qwerty is not a keyboard layout: is family of several dozen layouts. You should mention at least which country you mean. – Álvaro González Jul 28 '14 at 12:32
  • 1
    You need to check if Shift is also pressed. – Silviu Burcea Jul 28 '14 at 12:32
  • 1
    @ÁlvaroG.Vicario: And azerty isn't qwerty (though it's based on it): http://en.wikipedia.org/wiki/AZERTY – T.J. Crowder Jul 28 '14 at 12:32
  • @SilviuBurcea: *"But how to do with azerty keyboard, the both key have the same keycode and need shift."* – T.J. Crowder Jul 28 '14 at 12:33
  • This is easy, you either bind to the `keypress` event instead, which will give you different keycodes, or you check `event.shiftKey` – adeneo Jul 28 '14 at 12:38
  • if (shift is pressed) { someShiftFlag = true; } if (/ is pressed && someShiftFlag) { // ? is actually pressed } – Silviu Burcea Jul 28 '14 at 12:39
  • @SilviuBurcea: **Again**, the shift key is pressed for **both** of these characters on the azerty keyboard. – T.J. Crowder Jul 28 '14 at 12:58
  • @T.J.Crowder there is something specific that makes a difference between these keys. – Silviu Burcea Jul 28 '14 at 13:19
  • @SilviuBurcea: I'm sure there is, but it's not whether `Shift` is down as you keep suggesting, and whether the difference is in the information provided to JavaScript by the web browser for the `keyup` and `keydown` events (which are fairly raw) rather than the `keypress` event (translated) is something else entirely. It's no longer maintained, but [this page on the utter madness of keyboard events in JavaScript](http://unixpapa.com/js/key.html) is a good read. – T.J. Crowder Jul 28 '14 at 13:34

2 Answers2

5

Use the keypress event

$(document).keypress(function(event) {
    if (event.which === 666) {
        action();
    }
});

I don't have an azerty keyboard or whatever, so I don't get the same keycodes, but the keypress event will return other keycodes, you'll have to check them yourself.

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • The Keycodes are stored in the event. Why do they have different keycodes for the same key? Can you explain please.. – Daniel W. Jul 28 '14 at 12:43
  • `keyup` and `keydown` return different keycodes than `keypress`, so using the `keypress` event will return two different keycodes for the two characters, solving the issue. – adeneo Jul 28 '14 at 12:46
  • For a more thourough explanation, see [**this**](http://stackoverflow.com/questions/11030532/keypress-and-keyup-why-is-the-keycode-different) – adeneo Jul 28 '14 at 12:48
  • Wouldn't `event.charCode` be more reliable? – Ruan Mendes Jul 28 '14 at 12:48
  • @JuanMendes - jQuery normalizes `event.which`, so not really. – adeneo Jul 28 '14 at 12:48
  • Yes thanks you very much, with the keypress event the keycode are not the same for ? and / also on azerty keyboard – agauchy Jul 28 '14 at 12:48
  • @user3884214: Ah, okay, I thought you had a specific reason for using `keyup`. In general, for any keypress that results in a specific character (rather than arrows and such), you want to use `keypress`, not `keyup` and `keydown`, as `keypress` receives the character rather than a keycode. – T.J. Crowder Jul 28 '14 at 13:08
2

Check if shift is pressed

$(document).keyup(function(event) {
        if (event.which === 191 && event.shiftKey) {
            action();
        }
});

Note that this is keyboard layout dependent and it will be easier if you can use the keypress event as https://stackoverflow.com/a/24995506/227299 suggested

See http://unixpapa.com/js/key.html for further information

Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Yes but this with an azerty keyboard works for "?"and "/". Is there a solution to make a difference ? And thanks for your answer – agauchy Jul 28 '14 at 12:44