SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Keyboard keycode

    When key is pressed can jQuery tell which one?

    The backspace, delete, home or the arrow keys could they have a code when they are pressed?

    Code:
    $("#<%= TextBox1.ClientID %>").keypress(function () {
         if (character#="1111"){
              //code
         }
    });

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    jQuery simply wraps the event object which by default contains a property called which, this property contains the key code value based on the user input. See the below for how you can access the event object:

    Your code with the event object
    Code JavaScript:
    $("#<%= TextBox1.ClientID %>").keypress(function (e) {
         alert(e.which); // The key that was pressed
    });

    You can find a list of every key code at the following url http://www.cambiaresearch.com/articl...odes-key-codes.

    Depending on what version of jQuery you're using its recommended that you use the on method which handles delegation for the entire jQuery event wrapper.

    Code JavaScript:
    $('#<%= TextBox1.ClientID %>').on('keypress', function(e) {
         alert(e.which);
    });


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •