event.keyCode alternative

耗尽温柔 提交于 2019-12-01 22:36:25

问题


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:


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.



来源:https://stackoverflow.com/questions/29250534/event-keycode-alternative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!