How can I convert google chrome's .keyIdentifier to something reasonable?

别说谁变了你拦得住时间么 提交于 2019-12-11 10:35:55

问题


So I was doing a script that captures key presses. It's not so easy, because no browser seems to actually follow the specification of keyboard events.

In firefox, I can observe key codes (which determine specific physical buttons) in keyCode property while char codes (which represent letter of the key) in .charCode. This is not

When I tried to test in google chrome, I observed different behavior. Google chrome sets all keyCode, charCode and which to the letter value (if exists). However there's a constant value in keyIdentifier property. W3 says nothing about such property, but I'd be really happy to make use of it.

How can I convert keyIdentifier values (for A it's U+0041) to make them consistent with keyCode values appearing in Firefox (for A it's 65)?


回答1:


I haven't worked with keyboard events for Firefox or Chrome yet, but from the example you posted it seams like Chrome gives you the Unicode representation of the letter 'A' (where the number part is hexadecimal) while Firefox gives you the same number in base 10 (decimal). Making them consistent with each other should therefor be as simple as converting the Firefox number to hexadecimal so you can use the Unicode representation in both browsers.




回答2:


The actual code is:

//Google chrome retardedness
if(event.keyIdentifier) {
  keyCode = parseInt(event.keyIdentifier.substr(2), 16);
}
//not that the other browsers are any closer to something systematic and logical
else {
  keyCode = event.keyCode;
}


来源:https://stackoverflow.com/questions/27206737/how-can-i-convert-google-chromes-keyidentifier-to-something-reasonable

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