简体   繁体   English

js - 在不指定元素的情况下模拟按键

[英]js - simulate key press without specifying an element

i would like to simulate a key press in js.我想在js中模拟按键。 preferably without specifying any element.最好不指定任何元素。

the way i would like to do it is to use .focus() on an input and then simulate a key press like the character "a".我想做的方式是在输入上使用 .focus() 然后模拟像字符“a”这样的按键。

just the same thing that would happen if i would press the key myself.如果我自己按下键会发生同样的事情。

searching through the internet i found no solution.通过互联网搜索我没有找到解决方案。 it might have something to do with the combination of the simulated event and an input field.它可能与模拟事件和输入字段的组合有关。 sometimes i can catch the events, but there is no input present in the input field.有时我可以捕捉到事件,但输入字段中没有输入。

thank you very much in advance for any help.非常感谢您的帮助。

EDIT 1: this should not be considered a dublicate of: Is it possible to simulate key press events programmatically?编辑1:这不应该被认为是重复的: 是否可以以编程方式模拟按键事件?

because this does not solve my problem.因为这并不能解决我的问题。 i have already tried it.我已经试过了。

EDIT 2: please do not downvote this question just because you do not know how to answer it.编辑2:请不要仅仅因为您不知道如何回答这个问题而对这个问题投反对票。

If you have or can include jQuery, here's the easy way 如果您拥有或可以包括jQuery,这是简单的方法

With jQuery 使用jQuery

jQuery.event.trigger({ type: 'keydown', which: 78 }); // press n key

To simulate the keypress event within an input field, use like this 要模拟输入字段中的keypress事件,请像这样使用

var e = jQuery.Event("keydown");
e.which = 78; // n code value
e.altKey = true; // Alt key pressed
$("#inputBox").trigger(e);

Without jQuery 没有jQuery

var kbEvent= document.createEvent("KeyboardEvent");
var initMethod = typeof kbEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";

kbEvent[initMethod](
                   "keydown", // event type : keydown, keyup, keypress
                    true, // bubbles
                    true, // cancelable
                    window, // viewArg: should be window
                    false, // ctrlKeyArg
                    false, // altKeyArg
                    false, // shiftKeyArg
                    false, // metaKeyArg
                    78, // keyCodeArg : unsigned long the virtual key code , else 0
                    0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(kbEvent);

The above code infact will not modify the input value for you, it will only simulate keystrokes 上面的代码实际上不会为您修改输入值,它将仅模拟击键

See this post for more info How to simulate typing in input field using jQuery? 有关更多信息,请参见这篇文章。 如何使用jQuery模拟输入字段中的键入?

What you need is : fn.sendKeys 您需要的是: fn.sendKeys

I wrote this answer in Is it possible to simulate key press events programmatically?我在是否可以以编程方式模拟按键事件中写了这个答案 , which has been updated since this question has been asked (2018), and now also includes some info about updating the input element and triggering listeners (spoiler alert, it can be sometimes easy, sometimes harder, or sometimes not doable to my knowledge). ,自提出此问题(2018 年)以来已更新,现在还包括一些有关更新输入元素和触发侦听器的信息(剧透警报,有时可能很容易,有时更难,或者有时不可行) .

In regards to "not specifying an element", you need an eventTarget (like an element) for dispatchEvent .关于“不指定元素”,您需要一个eventTarget (如元素) dispatchEvent If you don't use any, then you're running it in theglobal object , equivalent to window.dispatchEvent() in the browser.如果你不使用任何,那么你在全局对象中运行它,相当于浏览器中的window.dispatchEvent()

Note: when you specify an element, the event can trigger listeners in other elements (parents) as well because of event bubbling注意:当您指定一个元素时,由于事件冒泡,该事件也可以触发其他元素(父级)中的侦听器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM