views:

781

answers:

6

Very straightforward:

Is it possible to prevent the browser from scrolling when a user pressed arrow keys?

A: 

Probably, but it's not usually a good idea to break a convention that everyone knows and understands :D

DrG
+6  A: 

yes. use something like: document.getElementById('yourID').onkeypress = HandleKeyPress;

function HandleKeyPress(e) {
    var e = e || window.event;
    switch (e.keyCode) {

     case e.DOM_VK_LEFT:
     case e.DOM_VK_RIGHT:
     case e.DOM_VK_UP:
     case e.DOM_VK_DOWN:
      if (e.preventDefault)
          e.preventDefault();
      else e.returnValue = false;
    }
}

Though it may not be a good idea to do so. just be sure that you're not overlooking a better approach.

Jonathan Fingland
I like the jQuery solution, i'm already using it.. Vote up
Ropstah
A: 

In JQuery

$("*").keypress(function(e){e.preventDefault(); return false;});
Antony Carthy
anyone asked "Is it possible to prevent document scrolling when arrow keys are pressed in jQuery?" ?
Hippo
This wouldn't let you press any key at all! No F5, no inputfields, nada!
peirix
I was giving the user an idea of how to do it... not the full solution.
Antony Carthy
A: 

in jQuery you could do:

$().keydown(function(e) { return e.keyCode != 38 && e.keyCode != 40; });
thatismatt
+6  A: 

Very straightforward: Yes, but don't do it.

Changing fundamental operation of how a browser operates just confuses or angers users, and makes the whole experience less user-friendly. The user should have ultimate control of the way his or her browser functions, not you. This is similar to blocking menu items from being accessed, or removing context menus etc.

Perspx
Google Finance drives me crazy by taking over the mouse wheel whenever my mouse pointer is over a chart. I _never_ get used to this.But there are times when it makes some sense to take over some control. Taking over the cursor keys for an in-browser game is pretty common. It's important that the user knows how to resume control--moving out of the game area should return cursor controls to default behavior.
Nosredna
Yes I was thinking about that scenario, which is understandable.. Although he didn't give background on what the behaviour was for so I was speaking generally.. Of course there are some situations where it is needed but it should be avoided unless absolutely necessary
Perspx
Clear :-) Thanks
Ropstah
A: 

It seems many are saying not to break what the browser does.

I would argue the same, unless you are replacing it with similar or better functionality.

I frequently kill the keyboard scroll in elements with an overflow because I've added a keyboard event to the element where using the keyboard "down" and "up" keys selects the items (think of a finder or windows explorer window).

The default scroll made the interaction all whacky. If the top item were selected, then you key 'down', it would select the next item but then scroll the element down and hide what was just selected.

So I break the default scroll, and then add my own to scroll to the selected item if it is below (or above) the current scrolling view. That way it behaves exactly as any OS out there when key-ing up and down through files.

Just saying, break it all you want, but make sure you know why you're breaking it and the user doesn't notice a thing.

rpflo