Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable arrow key scrolling in users browser

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.

like image 792
Kaninepete Avatar asked Jan 18 '12 20:01

Kaninepete


People also ask

How do I stop chrome from scrolling with arrow keys?

Click on the three vertical dots and select Settings. Go to Advanced > Accessibility. Turn off Navigate pages with a text cursor.


2 Answers

Summary

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); 

Original answer

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); 

References

  • MDN: window.addEventListener
  • MDN: window.removeEventListener
  • MDN: KeyboardEvent.code interface
like image 80
Zeta Avatar answered Sep 26 '22 20:09

Zeta


For 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:

  • https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/view
  • https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
like image 37
Yom T. Avatar answered Sep 22 '22 20:09

Yom T.