How to get right, left, up and bottom arrow key value in jquery

javascript, jquery, jquery-events

Solution

Use `keydown` insted of `keypress`

 $(document).keydown(function(event){    
    var key = event.which;                
            switch(key) {
              case 37:
                  // Key left.
                  break;
              case 38:
                  // Key up.
                  break;
              case 39:
                  // Key right.
                  break;
              case 40:
                  // Key down.
                  break;
        }   
  });

Problem

I have used the below code, and whenever click the arrow key (left, right, up, down) I get the key value is "0". Can anyone can help on this? ``` $(document).keypress(function (e) { alert("key value: " + e.which); }); ``` How to get (Up, Down, Right, Left) arrow key value when `keypress`.

Original source