2

I am trying to make a javascript function with jquery that tricks the device using my html page that a keyboard key is being pressed when you click a button on the page, I just have one problem, when I click the button it doesn't work and when i look in the console in Google Chrome it says Uncaught TypeError: undefined is not a function, here is the javascript:

function simulateKeyPress(keycode) {
  var evt = document.createEvent("KeyboardEvent");
  evt.initKeyEvent ("keypress", true, true, window,
                    0, 0, 0, 0,
                    0, keycode);
  var canceled = !body.dispatchEvent(evt); 
}

heres the html:

<div id="LeftArrow" onclick="simulateKeyPress(83)">
    <a href="#"><img src="PopularGamesImages/LeftArrow.jpg" width = "40px"/></a>
</div>

I would really apreciate it if anyone could help me with my problem, Thanks.

G.Mendes
  • 1,201
  • 1
  • 13
  • 18
  • According to the first comment in [this](http://stackoverflow.com/a/5920206/1130908) answer, Chrome doesn't have the `initKeyEvent`, but does support `initKeyboardEvent`. The answer looks like it was updated to support that as well. – Ben Felda Jun 05 '14 at 17:46
  • i changed it but when i click the button and check the google chrome console it just says "Uncaught ReferenceError: body is not defined" – Dylon Gallagher Jun 05 '14 at 23:15

0 Answers0