Skip to content

Instantly share code, notes, and snippets.

@ejoubaud
Last active April 9, 2024 18:38

Revisions

  1. ejoubaud revised this gist Oct 31, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion simulate_keypress.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    // Based on http://stackoverflow.com/a/10520017/1307721 and http://stackoverflow.com/a/16022728/1307721

    Podium = {};

    Podium.keydown = function(k) {
    var oEvent = document.createEvent('KeyboardEvent');

    @@ -29,4 +32,4 @@ Podium.keydown = function(k) {
    document.body.dispatchEvent(oEvent);
    }

    // Podium.keydown(40); for arrow-down
    Podium.keydown(40); // for arrow-down, arrow-up is 38
  2. ejoubaud created this gist Oct 31, 2014.
    32 changes: 32 additions & 0 deletions simulate_keypress.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    Podium = {};
    Podium.keydown = function(k) {
    var oEvent = document.createEvent('KeyboardEvent');

    // Chromium Hack
    Object.defineProperty(oEvent, 'keyCode', {
    get : function() {
    return this.keyCodeVal;
    }
    });
    Object.defineProperty(oEvent, 'which', {
    get : function() {
    return this.keyCodeVal;
    }
    });

    if (oEvent.initKeyboardEvent) {
    oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, k, k, "", "", false, "");
    } else {
    oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, k, 0);
    }

    oEvent.keyCodeVal = k;

    if (oEvent.keyCode !== k) {
    alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
    }

    document.body.dispatchEvent(oEvent);
    }

    // Podium.keydown(40); for arrow-down