Binding arrow keys in JS/jQuery

2018-12-31 14:24发布

How do I go about binding a function to left and right arrow keys in Javascript and/or jQuery? I looked at the js-hotkey plugin for jQuery (wraps the built-in bind function to add an argument to recognize specific keys), but it doesn't seem to support arrow keys.

16条回答
旧人旧事旧时光
2楼-- · 2018-12-31 14:31

You can use the keyCode of the arrow keys (37, 38, 39 and 40 for left, up, right and down):

$('.selector').keydown(function (e) {
  var arrow = { left: 37, up: 38, right: 39, down: 40 };

  switch (e.which) {
    case arrow.left:
      //..
      break;
    case arrow.up:
      //..
      break;
    case arrow.right:
      //..
      break;
    case arrow.down:
      //..
      break;
  }
});

Check the above example here.

查看更多
皆成旧梦
3楼-- · 2018-12-31 14:32

This is a bit late, but HotKeys has a very major bug which causes events to get executed multiple times if you attach more than one hotkey to an element. Just use plain jQuery.

$(element).keydown(function(ev) {
    if(ev.which == $.ui.keyCode.DOWN) {
        // your code
        ev.preventDefault();
    }
});
查看更多
残风、尘缘若梦
4楼-- · 2018-12-31 14:32

Instead of using return false; as in the examples above, you can use e.preventDefault(); which does the same but is easier to understand and read.

查看更多
看风景的人
5楼-- · 2018-12-31 14:34
$(document).keydown(function(e) {
    switch(e.which) {
        case 37: // left
        break;

        case 38: // up
        break;

        case 39: // right
        break;

        case 40: // down
        break;

        default: return; // exit this handler for other keys
    }
    e.preventDefault(); // prevent the default action (scroll / move caret)
});

Put your custom code for the arrow keys between the corresponding case and break lines.

e.which is normalized by jQuery, so it works in all browsers. For a pure javascript approach, replace the first two lines with:

document.onkeydown = function(e) {
    e = e || window.event;
    switch(e.which || e.keyCode) {


(edit 2017)
If you feel fancy, you can use e.key instead of e.which or e.keyCode now. e.key is becoming a recommended standard, allowing you to check against strings: 'ArrowLeft', 'ArrowUp', 'ArrowRight', 'ArrowDown'. New browsers support it natively, check here.

查看更多
荒废的爱情
6楼-- · 2018-12-31 14:36

I've simply combined the best bits from the other answers:

$(document).keydown(function(e){
    switch(e.which) {
        case $.ui.keyCode.LEFT:
        // your code here
        break;

        case $.ui.keyCode.UP:
        // your code here
        break;

        case $.ui.keyCode.RIGHT:
        // your code here
        break;

        case $.ui.keyCode.DOWN:
        // your code here
        break;

        default: return; // allow other keys to be handled
    }

    // prevent default action (eg. page moving up/down)
    // but consider accessibility (eg. user may want to use keys to choose a radio button)
    e.preventDefault();
});
查看更多
忆尘夕之涩
7楼-- · 2018-12-31 14:37
$(document).keydown(function(e){
    if (e.which == 37) { 
       alert("left pressed");
       return false;
    }
});

Character codes:

37 - left

38 - up

39 - right

40 - down

查看更多
登录 后发表回答