I have a Javascript function listening to keydown events and performs according to the key pressed.
My problem is as follows:
In Chrome, when I press "ö", which is a Turkish character with keyCode 246, event.keyCode returns 188 which is the code of the ma (,) character. (Firefox returns 0 but this is not problem for me since it is different from the ma.)
So my question is how can I distinguish character "ö" from the ma in Chrome? Or is there a way to get the original keyCode 246 on keydown event?
Thanks in advance.
I have a Javascript function listening to keydown events and performs according to the key pressed.
My problem is as follows:
In Chrome, when I press "ö", which is a Turkish character with keyCode 246, event.keyCode returns 188 which is the code of the ma (,) character. (Firefox returns 0 but this is not problem for me since it is different from the ma.)
So my question is how can I distinguish character "ö" from the ma in Chrome? Or is there a way to get the original keyCode 246 on keydown event?
Thanks in advance.
Share Improve this question asked Jul 8, 2013 at 14:11 ovunccetinovunccetin 8,7135 gold badges45 silver badges53 bronze badges 6-
Hmm, in Chrome
"ö".charCodeAt(0)
returns 246? If you print the character to the console, is it a ma or the ö? – altschuler Commented Jul 8, 2013 at 14:16 -
Why not use
event.charCode
onkeypress
? – Bergi Commented Jul 8, 2013 at 14:16 -
"ö".charCodeAt(0)
returns 246 for me also on Chrome v28.0.XX. Which Chrome version are you testing with? (ps.: Firefox 21.0 and 22.0 returns 246 also). – RaphaelDDL Commented Jul 8, 2013 at 14:22 - Definitive reading: unixpapa./js/key.html. You are confusing the code for the key (as in "keyboard") with the charCode from the character table. – Bergi Commented Jul 8, 2013 at 14:22
- I'm using Chromium version 18.0.1025.168 on Ubuntu 11.10. I know that the keypress event returns 246. However I have to handle the keydown event. – ovunccetin Commented Jul 8, 2013 at 14:33
3 Answers
Reset to default 2If you want to get the ASCII code you have to listen to the keypress
event and use event.charCode
.
The keydown
event does not provide the charCode
since there is a little difference between these two event types:
In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.
Source
You should use event.originalEvent.keyIdentifier
method. "ö" and ma has different keyIdentifiers.
Here is a good answer for these kind of problems. I see two rules here:
- to detect a character typed reliably, use keypress (with charCode)
- to detect non-printable characters such as arrows, use keydown (with keyCode)