Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6942559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:02:12+00:00 2026-05-27T13:02:12+00:00

is it possible to trigger a key event on the DOMWindow or DOMDocument in

  • 0

is it possible to trigger a key event on the DOMWindow or DOMDocument in javascript? Basically I am creating an browser extension to interact with a website and they have shortcut Key Events (similar to GMail) on performing specific actions. I have found other postings on how to properly trigger key events (Definitive way to trigger keypress events with jQuery) but they don’t seem to work when sending the key events to the document/window.

So far I have tried:

var evt = document.createEvent("KeyboardEvent");
evt.initKeyboardEvent("keydown", true, true, window, false, false, false, false, 0, "o".charCodeAt(0))
window.document.dispatchEvent(evt);

and a jQuery implementation:

$(document).trigger({ type: 'keydown', which: "0".charCodeAt(0) });

I also tried doing “keydown”, “keypress” and “keyup” in succession but that did not work either.

Any help would be greatly appreciated!

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:02:13+00:00Added an answer on May 27, 2026 at 1:02 pm

    I know you can pass extra arguments when using trigger() but I’m not sure you can overwrite properties of the event object. You can pass data like this though:

    $(document).on('keydown keyup keypress', function (event, characterCode) {
        if (typeof(characterCode) == 'undefined') {
            characterCode = -1;
        }
        console.log('type = ' + event.type);
        console.log('characterCode = ' + characterCode);
    });
    

    And this would be the trigger() code:

    $(document).trigger('keydown', [ "0".charCodeAt(0) ]);
    

    Here is a demo: http://jsfiddle.net/A7cEE/ (watch your console for the logs)

    The code from the stackoverflow answer you provided seems to work fine:

    To Capture:

    $(document).on('keydown', function (event) {
        console.log('type = ' + event.type);
        console.log('keyCode = ' + event.keyCode);
    });
    

    To Trigger:

    var e = jQuery.Event('keydown');
    e.keyCode = "0".charCodeAt(0);
    $(document).trigger(e);
    

    Demo: http://jsfiddle.net/A7cEE/1/

    Note that .on() is new in jQuery 1.7 and is the same as .bind() in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do you trigger javascript functions from flash? Possible Duplicate: How do
Is it possible to trigger for example flash uploading button via javascript? For example
I'm wondering, is it possible to somehow map a key-press event to act like
Is it possible to trigger a JavaScript function when a user tries to reload
Is is possible to trigger a link to select a single line inside textarea.
I'd like to know if it is possible to trigger programmatically the installation of
Is it possible to apply trigger on any table in information_schema?
Is it possible to create a trigger that will not be in a transaction?
Is it possible to to programmatically trigger a postback from server code in ASP.NET?
Is it possible to disable a trigger for a batch of commands and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.