Create KeyEvent in Javascript

I have the following problem: I deal with a key event, I need to create a new modified key event (since the keyCode property is read-only), and then it processes the newly created KeyEvent. I came across a few old posts in StackOverflow where similar situations are handled, but:

  • I need this to work under Webkit / there is a solution here in StackOverfow, but it only works in Gecko /

  • I need to create another KeyEvent, but not TextInputEvent, since TextInputEvent will only allow me to specify the string to be inserted, while I cannot do this, because I use a third-party tool that should handle this event and I need a key code.

  • I tried jQuery#trigger(), but this will not 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
    
+3
source share
1 answer
(function($){
    $(window).load(function(){

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

    });
})(jQuery);
+2
source

All Articles