0

I have this code

$(document.body).onkeyup = function(e) {
    var code = e.keyCode;
    if(code === 74) {
        window.scrollTo(document.body.scrollLeft,
                    document.body.scrollTop + 200);
    }
};

that allows me to scroll a page up and down with the keyboard. The problem is that I have to click on the page I want to scroll to activate this function.

Is there a way to activate immediately without the mouse click?

Thanks.

somethinghere
  • 16,311
  • 2
  • 28
  • 42
Federico
  • 1,392
  • 1
  • 17
  • 40

1 Answers1

1

As @somethinghere mentioned in the comments, the reason the shortcut doesn't work is because the document doesn't have focus.

You can attempt to force focus to document, but doing so is browser dependant:
Is there any way in JavaScript to focus the document (content area)?

Community
  • 1
  • 1
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115