Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.keyCode alternative

I am developing a virtual keyboard extension for Firefox. One of the features of this extension is converting the keys the user presses to another layout.

Example: I need to type text on Bashkir layout but my OS does not support it. So, I can use this virtual keyboard extension and not change the system settings.

It's needed on Windows XP which does not support many languages. I used event.keyCode to detect which key was pressed. I used it because it detects the key code despite the system layout.

But I missed that in Firefox if the keyboard is a non-English layout, event.keyCode doesn't work correctly with some keys, namely [ ] ; ' , . / and \.

If I type the q key, keyCode returns 81 and if I type in Russian й, which is the same key, keyCode also returns 81. But If I type the [ key, keyCode returns 219 and if I type in Russian х, which is the same key, keyCode returns 0 .

Do any keyCode alternatives exist?

like image 703
Ruslan Sayfullin Avatar asked Mar 25 '15 08:03

Ruslan Sayfullin


1 Answers

The available properties for KeyboardEvents are described on the linked page on MDN. They include:

  • KeyboardEvent.altKey
  • KeyboardEvent.charCode (Deprecated)
  • KeyboardEvent.code
  • KeyboardEvent.ctrlKey
  • KeyboardEvent.isComposing
  • KeyboardEvent.key
  • KeyboardEvent.keyCode (Deprecated)
  • KeyboardEvent.location
  • KeyboardEvent.metaKey
  • KeyboardEvent.shiftKey
  • KeyboardEvent.which (Deprecated)

KeyboardEvent.code appears to be the one which may provide you with the best information, and is not deprecated. However, it is only available from Firefox 32.0 in the Nightly, Aurora and Developer Editions of Firefox. From Firefox 38.0 and onwards it is, or will be, available in the standard release version.

You may have to experiment with various combinations of charCode, code, key, keyCode, and which to get the information you desire. If you desire to support a wide range of Firefox releases, you will almost certainly have to use a combination of different properties.

like image 103
Makyen Avatar answered Sep 17 '22 19:09

Makyen