Prevent scroll on HTML5 (SOLVED)



  • Hi,

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

    Thanks



  • 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) {
                    event.preventDefault();
                }
              }
    

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


Log in to reply