Created
March 21, 2014 22:06
-
-
Save ingorichter/9697477 to your computer and use it in GitHub Desktop.
Workaround to send KeyboardEvents in Webkit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This function helps to resolve an issue with sending Keyboard Events in Webkit | |
*/ | |
function sendKeyEvent(window, k) { | |
var oEvent = window.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, window.document.defaultView, false, false, false, false, k, k); | |
} else { | |
oEvent.initKeyEvent("keydown", true, true, window.document.defaultView, false, false, false, false, k, 0); | |
} | |
oEvent.keyCodeVal = k; | |
if (oEvent.keyCode !== k) { | |
alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")"); | |
} | |
window.document.dispatchEvent(oEvent); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment