Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

javascript - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...