Javascript Chrome 中的 KeyboardEvent,keyCode 为 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8942678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
KeyboardEvent in Chrome, keyCode is 0
提问by Andrija
I am trying to set keyCode on dispatched event object .
我正在尝试在调度的事件对象上设置 keyCode。
On button click, event is raised on textbox control to simulating keydown event. Event is raised successfully, but keyCode is 0. I need to send event object with correct keyCode to textbox. I've been using code from here.
单击按钮时,在文本框控件上引发事件以模拟 keydown 事件。事件已成功引发,但 keyCode 为 0。我需要将具有正确 keyCode 的事件对象发送到文本框。我一直在使用这里的代码。
How to set keyCode value in dispatched event?
如何在分派事件中设置 keyCode 值?
<html>
<head>
</head>
<body>
<input type="button" id="btn" value="Click"></input>
<input type="text" id="txt"></input>
<script>
document.getElementById('btn').addEventListener("click", btnClick);
document.getElementById('txt').addEventListener("keydown", txtKeydown);
function btnClick(e) {
var txt = document.getElementById('txt');
var dispatchKeyboardEvent = function(target, initKeyboradEvent_args) {
var e = document.createEvent("KeyboardEvents");
e.initKeyboardEvent.apply(e, Array.prototype.slice.call(arguments, 1));
e.keyCode = 83;
e.charCode = 0;
target.dispatchEvent(e);
};
var canceled = !dispatchKeyboardEvent(txt,
'keydown', true, true, // type, bubbles, cancelable
window, // window
's', // key
0, // location: 0=standard, 1=left, 2=right, 3=numpad, 4=mobile, 5=joystick
false, false, false); // Ctr, Alt, Shift
}
function txtKeydown(e) {
console.dir(e);
console.log(e.keyCode);
}
</script>
</body>
</html>
回答by Philip Nuzhnyy
I am amazed that this bug has been sitting there for ages. There seems to be a workaround using generic events. Here's a demo: http://jsbin.com/awenaq/3
我很惊讶这个错误已经存在多年了。似乎有一种使用通用事件的解决方法。这是一个演示:http: //jsbin.com/awenaq/3
回答by Justin Warkentin
It looks like it's been a bit since you asked this so you may have already found the answer to your question. In case you haven't though, there is a bug in Webkitas was pointed out in this SO questionthat causes the keyCode to always be 0. Apparently there's not much you can do until they fix it. Sadly it doesn't look like anyone is really working on it though. It's not the answer you want to hear, but it's really the only answer anyone can give you for this right now.
看起来您问这个问题已经有一段时间了,所以您可能已经找到了问题的答案。如果您还没有,那么正如在这个 SO question中指出的那样,Webkit 中存在一个错误,导致 keyCode 始终为 0。显然,在他们修复它之前,您无能为力。可悲的是,看起来并没有人真正在努力。这不是你想听到的答案,但它确实是现在任何人都可以给你的唯一答案。
回答by Raghu
I have faced same issue with Android devices like, Sony Xperia and HTC Devices. So i found alternate solution: please refer to my answer for this question KeyCode Returns 0 Always
我在使用索尼 Xperia 和 HTC 设备等 Android 设备时遇到了同样的问题。所以我找到了替代解决方案:请参考我对这个问题的回答 KeyCode Returns 0 Always
回答by medik
回答by mado
This happens because (as others have stated) the KeyboardEvent object (which uses charCode instead of keyCode) is being called instead of the Event object. When you are using:
发生这种情况是因为(正如其他人所说)正在调用 KeyboardEvent 对象(使用 charCode 而不是 keyCode)而不是 Event 对象。当您使用:
function txtKeydown(e) {
console.dir(e);
console.log(e.keyCode);
}
if you're getting a 0
printed to the console, try using e.charCode
instead of e.keyCode
如果您要0
打印到控制台,请尝试使用e.charCode
而不是e.keyCode
refer to MDN web docs for more information regarding KeyboardEvent: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
有关 KeyboardEvent 的更多信息,请参阅 MDN 网络文档:https: //developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
回答by fib Littul
It is not a bug... this feature is being weeded out, deprecated, forbidden, whateva' the name will be tomorrow... You cannot rely on this feature to work for long. ...and is a wide open security risk... find another way to do what you are trying to do. Consult the MOZILLA specs and you will see the BIG BAD RED BANNER which warns to avoid this like the plague. They will not fix it, by returning 0 they effectively stop the bleeding without changing the code except in one place where it always zaps the keyCode to 0.
这不是一个错误......这个功能正在被淘汰,弃用,禁止,明天会叫什么名字......你不能依赖这个功能长期工作。......而且是一个广泛的安全风险......找到另一种方式来做你正在尝试做的事情。查阅 MOZILLA 规范,您将看到 BIG BAD RED BANNER,它警告避免这种情况像瘟疫一样。他们不会修复它,通过返回 0,他们可以有效地停止流血而不更改代码,除非在一个地方它总是将 keyCode 设置为 0。