Can someone help me with this code. I am trying to simulate a keyboard input for a web page without specifically specifying the control to send it to. I am trying to do so through createEvent and dispatchEvent. but when I receive the actual keypressed the event does not have any reference to the character. The charCode, keyCode and which are always 0.
Thanks for your help! Max
<html>
<body>
<button type="button" onclick="KeyPressInject('e')">TEST</button>
</body>
<script type="text/javascript">
addEventListener("keypress", function(event){ EventKeyPressed(event)});
function KeyPressInject(letter)
{
var evt = document.createEvent("KeyboardEvent");
Object.defineProperty(evt, 'keyCode', {
get : function() {
return this.keyCodeVal;
} });
evt.initKeyboardEvent ("keypress", true, false, window, false, false, false, false, letter, letter);
var canc = !window.dispatchEvent(evt);
}
function EventKeyPressed(event)
{
console.log("Key Pressed " + event.charCode);
}
</script>
</html>