分配给 JavaScript keyCode 的实际键
我设置了一个事件监听器:
editor.addEventListener('keydown', function(e) {
if (e.shiftKey === false) {
alert(String.charFromCode(e.keyCode).toLowerCase());
}
else {
alert(String.charFromCode(e.keyCode));
}
}, false);
当用户按 2 和 shift 时,我如何知道是否应该输出(@)或(“)?每个用户的字符每个区域的映射都不同。
I have setup an event listener:
editor.addEventListener('keydown', function(e) {
if (e.shiftKey === false) {
alert(String.charFromCode(e.keyCode).toLowerCase());
}
else {
alert(String.charFromCode(e.keyCode));
}
}, false);
When the user presses 2 along with shift, how do I know if I should output (@) or (")? Each users' character mapping is different per locale.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请改用
keypress
事件。它将可靠地(除了一些边缘情况)检测键入的字符。有一些浏览器的奇怪现象(例如,某些不可打印的按键生成
keypress
事件,并且在某些浏览器中的which
属性中包含按键代码),导致以下示例无法达到 100 % 完美,您可以在 JavaScript 关键事件的权威页面 阅读详细信息。例子:
Use the
keypress
event instead. It will reliably (barring a few edge cases) detect the character typed.There are a few browser oddities (such as some non-printable keys generating
keypress
events with key codes in thewhich
property in some browsers) that prevent the following example from being 100% perfect which you can read about in great detail at the definitive page on JavaScript key events.Example:
简短的回答:你真的不能。使用
keypress
或keyup
侦听器,并将旧值(我猜是文本框?)与新值进行比较,以查看实际发生的情况。Short answer: you really can't. Use a
keypress
orkeyup
listener, and compare the old (textbox, I assume?) value to the new one to see what actually happened.