Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make difference between question mark ? and / for azerty keyboard

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

like image 251
agauchy Avatar asked Jul 28 '14 12:07

agauchy


2 Answers

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

like image 79
adeneo Avatar answered Oct 20 '22 01:10

adeneo


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

like image 21
Juan Mendes Avatar answered Oct 20 '22 00:10

Juan Mendes