Keyboard key codes detect with JQuery

JavaScript is one of the best Server Scripting Language for client side programming and JQuery is one of its library. JQuery is widely used by designers because it’s providing a lot of functionality just with few easy steps well I am not talking here about the designing part; we are talking about the keyboard key detection.

JavaScript Methods to Detect Keyboard keys:

There are three methods to detect the key codes of keyboard.

  1. keyup
  2. keydown
  3. keypress

If we are talking about the cross-browser compatible then keyup and keydown are the best solution for this.

Take a look for a quick example:

$(function() {
    $('input[name="set_text"]').keyup(function(e) {
        var code = (e.keyCode || e.which);

        if( code == 38 ) alert('Up Arrow');
        if( code == 40 ) alert('Down Arrow');
    });
});

 

HTML:

<input name="set_text" size="30" />

 

And here you can found the Keyboard Key code: Link