Binding arrow keys in JS/jQuery

后端 未结 16 1948
春和景丽
春和景丽 2020-11-22 12:25

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

相关标签:
16条回答
  • 2020-11-22 13:03

    I came here looking for a simple way to let the user, when focused on an input, use the arrow keys to +1 or -1 a numeric input. I never found a good answer but made the following code that seems to work great - making this site-wide now.

    $("input").bind('keydown', function (e) {
        if(e.keyCode == 40 && $.isNumeric($(this).val()) ) {
            $(this).val(parseFloat($(this).val())-1.0);
        } else if(e.keyCode == 38  && $.isNumeric($(this).val()) ) { 
            $(this).val(parseFloat($(this).val())+1.0);
        }
    }); 
    
    0 讨论(0)
  • 2020-11-22 13:04

    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();
        }
    });
    
    0 讨论(0)
  • 2020-11-22 13:04

    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();
    });
    
    0 讨论(0)
  • 2020-11-22 13:04

    You can use KeyboardJS. I wrote the library for tasks just like this.

    KeyboardJS.on('up', function() { console.log('up'); });
    KeyboardJS.on('down', function() { console.log('down'); });
    KeyboardJS.on('left', function() { console.log('right'); });
    KeyboardJS.on('right', function() { console.log('left'); });
    

    Checkout the library here => http://robertwhurst.github.com/KeyboardJS/

    0 讨论(0)
  • 2020-11-22 13:06

    prevent arrow only available for any object else SELECT, well actually i haven't tes on another object LOL. but it can stop arrow event on page and input type.

    i already try to block arrow left and right to change the value of SELECT object using "e.preventDefault()" or "return false" on "kepress" "keydown" and "keyup" event but it still change the object value. but the event still tell you that arrow was pressed.

    0 讨论(0)
  • 2020-11-22 13:08

    You can check wether an arrow key is pressed by:

    $(document).keydown(function(e){
        if (e.keyCode > 36 && e.keyCode < 41) { 
           alert( "arrowkey pressed" );
           return false;
        }
    });
    
    0 讨论(0)
提交回复
热议问题