简体   繁体   English

以编程方式模拟空格键然后在文本输入字段上退格

[英]Programmatically simulate spacebar then backspace on a text input field

I want to dispatch user actions on a text input field.我想在文本输入字段上分派用户操作。 I want it to be as if an actual person has used their keyboard to click a text input field, hit the spacebar, and then hit the backspace key.我希望它就像一个真实的人使用他们的键盘点击文本输入字段,点击空格键,然后点击退格键。 With the code I used nothing has happened.使用我使用的代码没有任何反应。

THE JSFIDDLE https://jsfiddle.net/zvrhfq9j/ JSFIDDLE https://jsfiddle.net/zvrhfq9j/

THE HTML HTML

<input type="text" name="psychTree-savedNodes" style="width:20%" />

THE JS JS

$('input[name="psychTree-savedNodes"]').focus();
$('input[name="psychTree-savedNodes"]').trigger({type: 'keypress', which: 32, keyCode: 32});
$('input[name="psychTree-savedNodes"]').trigger({type: 'keyup', which: 32, keyCode: 32});
$('input[name="psychTree-savedNodes"]').trigger({type: 'keydown', which: 32, keyCode: 32});
$('input[name="psychTree-savedNodes"]').trigger({type: 'keypress', which: 8, keyCode: 8});
$('input[name="psychTree-savedNodes"]').trigger({type: 'keyup', which: 8, keyCode: 8});
$('input[name="psychTree-savedNodes"]').trigger({type: 'keydown', which: 8, keyCode: 8});

THE SOLUTION: was to actually dispatch the events.解决方案:实际调度事件。 Thanks for those that actually answered!感谢那些真正回答的人!

You can simulate user actions, but they won't perform the default functions because it will set isTrusted to false (for security reasons).可以模拟用户操作,但它们不会执行默认功能,因为它会将isTrusted设置为 false(出于安全原因)。

For instance, you can build an event to dispatch to a text field that "types the letter 'a'".例如,您可以构建一个事件以分派到“键入字母‘a’”的文本字段。 It will (err, should ) trigger any custom functions bound to that event handler ( el.onkeydown(e){ if( e.key == 'a' ) … ), but it will not type the letter a into the text field, or otherwise process default browser functionality based on that keystroke.它将(错误,应该)触发绑定到该事件处理程序的任何自定义函数( el.onkeydown(e){ if( e.key == 'a' ) … ),但它不会在文本字段中键入字母a ,或以其他方式处理基于该击键的默认浏览器功能。

It's a browser implementation, and not something you can get around.这是一个浏览器实现,而不是你可以绕过的东西。 So while you can't "type" directly into fields, you can run events based off the event handlers that are attached to those specific events.因此,虽然您不能直接在字段中“键入”,但您可以根据附加到这些特定事件的事件处理程序来运行事件。

I've whipped up a codepen example to show what I mean: https://codepen.io/xhynk/pen/jOPbWzz我举了一个代码笔示例来说明我的意思: https://codepen.io/xhynk/pen/jOPbWzz

  • The page loads blue, with 2 fields页面加载为蓝色,包含 2 个字段
  • In 1 second, it will run a function that "simulates" a "click > space > backspace" chain of events.在 1 秒内,它将运行一个 function 来“模拟”“点击 > 空格键 > 退格键”事件链。
  • The events will display what they did inside the "no" input这些事件将显示他们在“否”输入中所做的事情
  • The page turns green to show it went off.页面变为绿色表示它已关闭。

If you're so inclined, you can change the event codes to letters to see that the actual keystrokes never appear in the "yes" input .如果您愿意,可以将事件代码更改为字母,以查看实际击键不会出现在“是”输入中。 I've added the "no" box that has keydown and onclick event handlers to show what events fired and in what order by changing the value of it.我已经添加了具有keydownonclick事件处理程序的“否”框,以通过更改它的value来显示触发了哪些事件以及触发的顺序。

You can also manually click on the "yes" input, then hit "space" and "backspace" and see they too fire the event handler functions (the event functions will run like they did when they were simulated) but this time they will actually show up in the box (because you actually did them, so they are trusted events).您也可以手动单击“是”输入,然后单击“空格”和“退格键”,看看它们是否也触发了事件处理函数(事件函数将像模拟时那样运行),但这次它们实际上出现在框中(因为你确实做到了,所以它们是可信事件)。

Long story short, you can't fully simulate "typing" keypress events that are trusted, but you can handle those specific keypress events however you want (even changing the value of the current input if you so choose).长话短说,您不能完全模拟受信任的“键入”按键事件,但您可以根据需要处理这些特定的按键事件(如果您愿意,甚至可以更改当前输入的值)。

Edit: I see a chain of comments has spawned up at the top.编辑:我看到顶部出现了一系列评论。 This is a browser function that you can't get around, unless you find (or build) a browser that handles untrusted isTrusted:false events.这是一个您无法绕过的浏览器function,除非您找到(或构建)一个处理不受信任的isTrusted:false事件的浏览器。 This doesn't have anything to do with local or sandbox programming environments.这与本地或沙盒编程环境无关。

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

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