Binding arrow keys in JS/jQuery

后端 未结 16 2194
春和景丽
春和景丽 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: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();
        }
    });
    

提交回复
热议问题