I'm using JavaScript and Prototype and catching the key presses from the user. I successfully catch return, space and arrows with code like this:
Event.observe(window, "keyup", function(e) {
switch (e.keyCode) {
case Event.KEY_RETURN:
case Event.KEY_RIGHT:
case 32: // space
// do something
break;
}
});
My problem is that spaces and arrow keep on scrolling the page. Is there a way to keep them from scrolling the page?
It's too late in
keyup
to prevent the default browser action. Do it in thekeydown
event instead and use Prototype'sEvent.stop
method:e.preventDefault() works in Chrome.
Use
e.preventDefault()
to Stop default behavior of browserFrom the Prototype documentation:
So adding
Event.stop(e);
before thebreak;
should solve your problem.Also, you should be doing this for the
keydown
event, becausekeyup
is too late.