automatically press keyboard with jQuery?

前端 未结 6 1925
臣服心动
臣服心动 2021-01-19 04:06

Is it possible with jQuery to automatically simulate a press on a keyboard, f.ex. inside an html input field?

As explanation: If I press the a inside an

6条回答
  •  攒了一身酷
    2021-01-19 04:24

    trigger does help, because it fires an event...and that is the only thing you are interested in browser...everything before that is a matter of OS (which you cannot reach, since you are building inside a browser...unless you build some activeX control). So, this should work:

    var e = $.Event("keypress");
    e.which = ;
    $("").trigger(e);
    

    As you can see here keypress event is sent when browser registers keyboard input. Difference between keypress and keydown is: "If the user presses and holds a key, a keydown event is triggered once, but separate keypress events are triggered for each inserted character...". You can decide which one you will use depending on your use case.

提交回复
热议问题