3

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
    

1 Answer 1

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

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

    });
})(jQuery);
Sign up to request clarification or add additional context in comments.

4 Comments

I have not the time to create a test example. Just give it a try. Thanks
nope, for some reason this wont work, the iframe's body is found but the event is not triggered or triggering it leads to nothing. No errors are produced in the log.
I went ahead and used TextInputEvent and document.dispatchEvent(). Seems working.
@asenovm Great! I'm glad you did it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.