jQuery - Keystrokes

The jQuery event.which will return which keyboard key was pressed.

Type a character in the textbox. Watch what happens when you press the key; hold the key down, and the release the key.

 

There are a few events to keep in mind when you are trying to detect and validate input which are:

  • .keydown() - all keys trigger this...even function keys like Shift, Enter, Delete, etc.
  • .keypress() - excludes non-printing keys such as Shift, Enter, Delete, etc.
  • .keyup() - triggered when a key is released.

http://www.w3schools.com/jquery/event_which.asp

http://api.jquery.com/keydown/

http://api.jquery.com/keypress/

http://api.jquery.com/keyup/


$(document).ready(function ()
{
    $("#txtDemo").keydown(function (event)
    {
        console.dir(event);
        $("#demo1").html("Key: " + event.which + " was pressed down.");
    });

    $("#txtDemo").keyup(function (event)
    {
        console.dir(event);
        $("#demo2").html("Key: " + event.which + " was released.");
    });
});