event.keyCode alternative

2020-04-11 01:21发布

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?

1条回答
等我变得足够好
2楼-- · 2020-04-11 02:17

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

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.

查看更多
登录 后发表回答