Simulate a keyboard key when you lose focus

2

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

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

Follow link with another post from mine which has related images.

    
asked by anonymous 28.03.2014 / 13:18

2 answers

2

Use the event blur on the element that triggers the "Enter" when it loses focus. And in catching the event, trigger a keypress event with your own information using the trigger .

More or less 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 the Enter pressed on the same component that lost focus. You can use another component, according to your need;)

    
28.03.2014 / 14:54
0

You can not simulate a specific key, just an event.

Perhaps if you can be more specific with your need there may be other solutions.

If you want to trigger a function either by pressing enter or by taking the focus from the field (if it works), something like this might 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.

    }

});
    
28.03.2014 / 13:21