Prevent scroll on HTML5 (SOLVED)

  • Hi,

    I uploaded a HTML5 game to but, trying to play it, when I press the game controls this makes the page to scroll. Anybody knows woh could I avoid that?


  • I found the solution.

    I had to add onkeydown="preventUseOfDefaultKeys(event);"on the body and define the function like this:

    function preventUseOfDefaultKeys(event) {
                if (event.keyCode == 32 || event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) {

    This prevents the scrolling when pressing the arrow keys and spacebar. For other keys, change keyCodes.

  • I wish I had found this earlier, would have saved me a lot of time. I had to do something similar to the index.html file (you didn't specify if this is what you were doing).

Log in to reply