Simulate a keyboard key when losing focus

Asked

Viewed 1,613 times

2

I would like to know how to do a simulation of a keyboard key (in my case the enter key), but in the case in Jquery.

But it has to simulate when leaving an input for example.

Follows link with another post of mine that has images related.

  • what do you mean by "simulate" ?

  • because it does not fire whatever it has to shoot at enter also at loss of focus of field ?

  • Well I will explain what I want to do, I am working with Jqgrid, and when the user is performing an edit (inline) the Mbo is enabled and it saves the information by pressing the Enter key, in this case I would like that when the user loses the focus of the field, were simulated the Enter key.

2 answers

2

Use the event blur in the element that fires "Enter" when losing focus. And in the event capture, fire an event keypress with own information through the method trigger.

Sort of like this:

foo.blur(function () {
    var keypress = jQuery.Event("keypress");
    keypress.which = 13; // 13 é o codigo da tecla Enter
    keypress.keyCode = 13; // vide linha acima
    $(this).trigger(keypress);
});

Note that the above code simulates Enter pressed on the same component that lost focus. You can use another component according to your need ;)

  • In expensive the "foo" would be my object? I am testing here and return soon.

  • Yes. foo is any element, which you have already obtained via jQuery. In code examples, it is normal to use the names foo and bar for the variables ;)

  • Thank you, I’m testing here and I’ll be right back.

  • I couldn’t simulate the key, I don’t know if it’s because I’m using Jqgrid and it doesn’t work, but I performed a function that generates what I need when I lose focus on the object.

0

Cannot simulate a specific key, only an event.

Perhaps if you could be more specific with your need there might be other solutions.

If you want to fire a function either by pressing enter or by taking the focus out of the field (If solve), something like this can help:

$('#id-do-seu-campo').on('keypress blur', function(event) {

    if((event.type == 'keypress' && event.keyCode == 13) || event.type == 'blur') {

        // Aqui você faz o quer que aconteça no pressionar do enter.

    }

});
  • Well, I’m using Jqgrid and it saves the information on the server when the enter key is pressed, but instead of sending the information to the server it sends the complete object, according to the link I posted http://answall.com/questions/5514/jqgrid-error-to-save-field-data-via-datepicker

  • I think that answer would be better as a comment...

  • Well I will explain what I want to do, I am working with Jqgrid, and when the user is performing an edit (inline) the Mbo is enabled and it saves the information by pressing the Enter key, in this case I would like that when the user loses the focus of the field, were simulated the Enter key

  • What I intend to do is the same as a robot class does in java, but making jquery have keyboard control, in the case of just the enter key

  • If I understand better, I believe you can delegate events blur and keyup, and within keyup check whether event.keyCode is equal to 13, which corresponds to enter key, if you delegate the two events and in the function do the action you want I believe will have the same effect and would not need to "simulate" a key...

  • Diego, would you have an example? I’ll research these events.

  • Can I try to do something, are you using jQuery in your project? If you’re making it easy...

  • Yes it is Jquery! Thank you

  • I’ll have to put it in the answer because it’s a code block. I’m editing it.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.