I'm making a game using canvas, and javascript.
When the page is longer than the screen (comments, etc.) pressing the down arrow scrolls the page down, and makes the game impossible to play.
What can I do to prevent the window from scrolling when the player just wants to move down?
I guess with Java games, and such, this is not a problem, as long as the user clicks on the game.
I tried the solution from: How to disable page scrolling in FF with arrow keys ,but I couldn't get it to work.
Click on the three vertical dots and select Settings. Go to Advanced > Accessibility. Turn off Navigate pages with a text cursor.
Simply prevent the default browser action:
window.addEventListener("keydown", function(e) { if(["Space","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) { e.preventDefault(); } }, false);
If you need to support Internet Explorer or other older browsers, use e.keyCode
instead of e.code
, but keep in mind that keyCode
is deprecated and you need to use actual codes instead of strings:
// Deprecated code! window.addEventListener("keydown", function(e) { // space and arrow keys if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) { e.preventDefault(); } }, false);
I used the following function in my own game:
var keys = {}; window.addEventListener("keydown", function(e){ keys[e.code] = true; switch(e.code){ case "ArrowUp": case "ArrowDown": case "ArrowLeft": case "ArrowRight": case "Space": e.preventDefault(); break; default: break; // do not block other keys } }, false); window.addEventListener('keyup', function(e){ keys[e.code] = false; }, false);
The magic happens in e.preventDefault();
. This will block the default action of the event, in this case moving the viewpoint of the browser.
If you don't need the current button states you can simply drop keys
and just discard the default action on the arrow keys:
var arrow_keys_handler = function(e) { switch(e.code){ case "ArrowUp": case "ArrowDown": case "ArrowLeft": case "ArrowRight": case "Space": e.preventDefault(); break; default: break; // do not block other keys } }; window.addEventListener("keydown", arrow_keys_handler, false);
Note that this approach also enables you to remove the event handler later if you need to re-enable arrow key scrolling:
window.removeEventListener("keydown", arrow_keys_handler, false);
window.addEventListener
window.removeEventListener
KeyboardEvent.code
interfaceFor maintainability, I would attach the "blocking" handler on the element itself (in your case, the canvas).
theCanvas.onkeydown = function (e) { if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { e.view.event.preventDefault(); } }
Why not simply do window.event.preventDefault()
? MDN states:
window.event
is a proprietary Microsoft Internet Explorer property which is only available while a DOM event handler is being called. Its value is the Event object currently being handled.
Further readings:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With