Disable arrow key scrolling in users browser

后端 未结 3 1112
盖世英雄少女心
盖世英雄少女心 2020-11-29 20:46

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 th

3条回答
  •  青春惊慌失措
    2020-11-29 21:32

    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

提交回复
热议问题