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

javascript - jQuery up keydown key keypress detection not working? - Stack Overflow

programmeradmin2浏览0评论

In the following code:

$(document).keypress(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 40) {
            alert("down pressed");
        } else if (code == 38) {
            alert("up pressed");
        }
    });

I'm trying to detect if the down key or up key is pressed. Why isn't it working?

Fiddle /

I'm in chrome

In the following code:

$(document).keypress(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 40) {
            alert("down pressed");
        } else if (code == 38) {
            alert("up pressed");
        }
    });

I'm trying to detect if the down key or up key is pressed. Why isn't it working?

Fiddle http://jsfiddle.net/K9uDn/10/

I'm in chrome

Share Improve this question edited Jul 1, 2013 at 19:23 SB2055 asked Jul 1, 2013 at 19:18 SB2055SB2055 12.9k37 gold badges104 silver badges205 bronze badges 6
  • The event isn't firing. – crush Commented Jul 1, 2013 at 19:22
  • 1 Well, it works if I change keypress to keydown or keyup (and if I remove those self.gotIt() and self.forgotIt()). I strongly recommend that you use keyup or keydown anyways, even if you didn't have this issue... it's much better, and everything is predictable with it. keydown should replace keypress as well. – MiJyn Commented Jul 1, 2013 at 19:24
  • 2 jQuery normalizes keyCode and charCode with which, so you don't need to check them both. Just use which. – crush Commented Jul 1, 2013 at 19:26
  • You're jsFiffle works but to make it work you need focus on the element. Thus clicking the box in the right bottom will make your script work on jsFiddle. jQuery quote: "A keypress event handler can be attached to any element, but the event is only sent to the element that has the focus." – Michael Commented Jul 1, 2013 at 19:29
  • @Michael What browser are you using? That doesn't work for me in Chrome. I think keypress is more of an IE thing. – crush Commented Jul 1, 2013 at 19:29
 |  Show 1 more comment

4 Answers 4

Reset to default 21

Use keydown instead of keypress, some browsers does not fire keypress when an "special key (like arrows)" are pressed

jQuery's keypress method isn't very stable. Use on('keydown') instead.

See: http://jsfiddle.net/K9uDn/9/

Here's the Fiddle of a fixed version of the javascript code with extra buttons trapped using a switch/case statement

$(document).on('keydown',function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        switch (code){
          case 34:
            alert("Page down pressed");
            break;
          case 33:
            alert("Page up pressed");
            break;
          case 40:
            alert("Down pressed");
            break;
          case 38:
            alert("Up pressed");
            break;
          case 37:
            alert("Left pressed");
            break;
          case 39:
            alert("Right pressed");
            break;
        }

on('keyup') does not work in IE. Not in version 9 at least.

发布评论

评论列表(0)

  1. 暂无评论