[英]How can I pass two keydown events at the same time with JavaScript?
Is there a way using jQuery or vanilla JS that I can pass down 2 keystrokes?有没有一种使用 jQuery 或香草 JS 的方法可以传递 2 次击键?
Example: When I press the space key as an event listener, both the enter and shift key should get pressed and passed down together.示例:当我按下空格键作为事件监听器时,回车键和 shift 键都应该被按下并一起向下传递。 I managed for the code to run, but for one single key stroke.
我设法让代码运行,但只需要一个按键。 Can someone shine some light on this?
有人可以对此有所了解吗?
$(document).keydown(function(event) {
var keycode = (event.keyCode ? event.keyCode : event.which);
if (keycode == 13) {
document.addEventListener('keydown', function(ev) {
console.log(ev.which);
});
var e = new KeyboardEvent('keydown', {
'keyCode': 190,
'which': 190
});
console.log(e);
document.dispatchEvent(e);
}
});
To add modifiers like shift
, ctrl
, or alt
to a KeyPress event, you can pass those properties in the second constructor argument.要将诸如
shift
、 ctrl
或alt
之类的修饰符添加到 KeyPress 事件,您可以在第二个构造函数参数中传递这些属性。 See the documentation here .请参阅此处的文档。
// ex. Shift+Enter
var e = new KeyboardEvent("keydown", {
key: "Enter",
shiftKey: true,
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.