Quantcast
Viewing latest article 3
Browse Latest Browse All 4

Binding arrow keys in JS/jQuery

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.


Solution:

$(document).keydown(function(e){
    if (e.keyCode == 37) { 
       alert( "left pressed" );
       return false;
    }
});

Character codes:

37 – left

38 – up

39 – right

40 – down


Viewing latest article 3
Browse Latest Browse All 4

Trending Articles