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.)

9
  • 1
    Qwerty is not a keyboard layout: is family of several dozen layouts. You should mention at least which country you mean. Commented Jul 28, 2014 at 12:32
  • 1
    You need to check if Shift is also pressed. Commented Jul 28, 2014 at 12:32
  • 1
    @ÁlvaroG.Vicario: And azerty isn't qwerty (though it's based on it): en.wikipedia.org/wiki/AZERTY Commented Jul 28, 2014 at 12:32
  • @SilviuBurcea: "But how to do with azerty keyboard, the both key have the same keycode and need shift." Commented Jul 28, 2014 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 Commented Jul 28, 2014 at 12:38

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

The Keycodes are stored in the event. Why do they have different keycodes for the same key? Can you explain please..
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.
Wouldn't event.charCode be more reliable?
For a more thourough explanation, see this
@JuanMendes - jQuery normalizes event.which, so not really.
|
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

1 Comment

Yes but this with an azerty keyboard works for "?"and "/". Is there a solution to make a difference ? And thanks for your answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.