Event.keyCode returns a comma (188) value for the Turkish character "ö"

I have a Javascript function that listens for keydown events and executes according to the key pressed.

My problem is this:

In Chrome , when I press " ö ", which is a Turkish character with keyCode 246 , event.keyCode returns 188 , which is the comma character code (,). (Firefox returns 0, but this is not a problem for me, since it is different from a comma.)

So my question is: how to distinguish the character "ö" from the comma in Chrome? Or is there a way to get the source code of KeyCode 246 on a keydown event?

Thanks in advance.

+4
source share
3 answers

If you want to get the ASCII code, you need to listen to the keypress event and use event.charCode .

The keydown event keydown not provide charCode , as there is a slight difference between the two types of events:

In theory, keydown and keyup events are keys that are pressed or released, and a keypress event is a typed character. The implementation of the theory is not the same in all browsers.

A source

+2
source

You must use the event.originalEvent.keyIdentifier method. "ö" and comma have different key identifiers.

+1
source

Here is a good answer to these problems. Here I see two rules:

  • to detect a character typed securely, use keypress (with charCode)
  • to detect non-printable characters such as arrows, use keydown (with keyCode)
+1
source

Source: https://habr.com/ru/post/1490298/


All Articles