最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get right, left, up and bottom arrow key value in jquery - Stack Overflow

programmeradmin9浏览0评论

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.

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.

Share Improve this question edited Oct 5, 2022 at 18:42 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 21, 2013 at 12:40 BharathiBharathi 1,3282 gold badges14 silver badges45 bronze badges 1
  • possible duplicate of jQuery Keypress Arrow Keys – James Donnelly Commented Nov 21, 2013 at 12:51
Add a ment  | 

2 Answers 2

Reset to default 5

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;
        }   
  });

In many browsers the keypress event doesn't recognise the arrow keys (nor keys like "shift", "del", "esc", etc. If you want to capture those key presses, you'll need to use keydown or keyup instead:

$(document).keydown(function (e) {

    alert("key value: " + e.which); 

});

JSFiddle demo.

As for the right, left, up and down arrow keys, those are:

left     37
up       38
right    39
down     40
发布评论

评论列表(0)

  1. 暂无评论