Binding arrow keys in JS/jQuery

后端 未结 16 2118
春和景丽
春和景丽 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条回答
  •  Happy的楠姐
    2020-11-22 13:26

    You can use jQuery bind:

    $(window).bind('keydown', function(e){
        if (e.keyCode == 37) {
            console.log('left');
        } else if (e.keyCode == 38) {
            console.log('up');
        } else if (e.keyCode == 39) {
            console.log('right');
        } else if (e.keyCode == 40) {
            console.log('down');
        }
    });
    

提交回复
热议问题