Simulate Keypress with Jquery

Simulate Keypress With jQuery

The keypress event from jQuery is meant to do this sort of work. You can trigger the event by passing a string "keypress" to .trigger(). However to be more specific you can actually pass a jQuery.Event object (specify the type as "keypress") as well and provide any properties you want such as the keycode being the spacebar.

http://docs.jquery.com/Events/trigger#eventdata

Read the above documentation for more details.

jQuery simulate keypress on input

The problem seems to be order of your code. Your code which triggers keypress is defined before the event handler.

This seems to work now. (JSFiddle)

$('input').on('keypress', function(){
$('p').text("worked");
});

$('input').val('test').trigger(jQuery.Event('keypress', {keycode: 13}));

UPDATE:
Also Please don't use keycode as it is deprecated (link) and might not work in some cases. I've personally faced this issue. Use which instead.

JSFiddle

$('input').on('keypress', function(){
$('p').text("worked");
});

var e = jQuery.Event("keypress");
e.which = 13;
$("input").focus().val('test').trigger(e);

Simulate keypress using jQuery

you can try this one:

You can create an Event object

$('#canvas').trigger(jQuery.Event('keypress', {keyCode: 87, which: 13}));

You maybe have to try which parameter to set, e.g. keyCode.

or

The real answer has to include keyCode:

var e = jQuery.Event("keydown");
e.which = 50; // # Some key code value
e.keyCode = 50
$("input").trigger(e);

Even though jQuery's website says that which and keyCode are normalized they are very badly mistaken. It's always safest to do the standard cross-browser checks for e.which and e.keyCode and in this case just define both.

or

jQuery has a .keypress method accepting no arguments that simulates a keypress.

$("#canvas").keypress();

Will trigger a keypress on #canvas

If you'd like to also select which key was pressed, you can use .trigger. This example is from the Docs.

var e = $.Event("keydown", { keyCode: 87}); //"keydown" if that's what you're doing
$("body").trigger(e);

The key code 87 is the Key code backspace in javascript

Let me know how that works for you :)

My demo pages:

DEMO 1

DEMO 2

Definitive way to trigger keypress events with jQuery

If you want to trigger the keypress or keydown event then all you have to do is:

var e = jQuery.Event("keydown");
e.which = 50; // # Some key code value
$("input").trigger(e);

How can I simulate a keypress in JavaScript?

As @rfsbsb pointed out from: Jquery script for simulated key press down not running keyboard shortcut

If you're trying to fire some browser or system wide keyboard shortcut
then it's a dead end - it can't be done for security reasons. If it
would be possible, you would have pages all over the Internet that
would (for example) add themself to your bookmarks without even asking
(by firing CTRL+B shortcut using Javascript).

JQuery simulating keypress event on an input field

You can create an event object:

$('#myInputId').trigger(jQuery.Event('keypress', { keycode: 13 }));

Simulate multiple keypress using jquery

It works, if you run the snippet and see 78 inside the content (78 is the keycode of N).

<script src="https://code.jquery.com/jquery-3.1.0.js"></script><script>  $(function () {    $("body").keypress(function (e) {      $(this).html(e.which);    });    var e = jQuery.Event("keypress");    e.ctrlKey = true;    e.shiftKey = true;    e.keyCode = e.which = 78;    $("body").trigger(e);  });</script>


Related Topics



Leave a reply



Submit