Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create KeyEvent in Javascript

I have the following problem - I'm catching a key event an I need to create a new altered key event(since it seems the keyCode property is read-only) and afterwards handle the newly created KeyEvent. I came across several old posts in StackOverflow where similar situations are handled, but:

  • I need this to be working under Webkit /there's a solution here in StackOverfow but it is working only in Gecko/

  • I need to create another KeyEvent, but not TextInputEvent, since the TextInputEvent will only let my specify a string to be inserted, whilst I cannot do that as I use a third party tool that needs to handle this event and I need a keycode.

  • I tried jQuery#trigger() but it won't work for me. My code is as follows

    var event = jQuery.event('keydown');
    event.which = 13; //I'm trying to simulate an enter
    $('iframe').contents().find('document').find('body').trigger(event); //my content is inside an iframe
    
like image 830
asenovm Avatar asked Aug 13 '26 08:08

asenovm


1 Answers

(function($){
    $(window).load(function(){

        var e = $.Event("keydown");
        e.which = 13;
        e.keyCode = 13;
        $('iframe').contents().find('html, body').trigger(e);

    });
})(jQuery);
like image 141
Roko C. Buljan Avatar answered Aug 15 '26 09:08

Roko C. Buljan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!