jQuery API

event.which

event.which Returns: Number

Description: For key or button events, this attribute indicates the specific button or key that was pressed.

  • version added: 1.1.3event.which

event.which normalizes event.keyCode and event.charCode. It is recommended to watch event.which for keyboard key input. For more detail, read about event.charCode on the MDC.

Example:

Log what key was depressed.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
<input id="whichkey" value="type something">
<div id="log"></div>
<script>$('#whichkey').bind('keydown',function(e){ 
  $('#log').html(e.type + ': ' +  e.which );
});  </script>

</body>
</html>

Demo:

Result:

"keydown" 74  

Support and Contributions

Need help with event.which or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to event.which? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Rigo

    I think that for 1.4.1, or perhaps earlier, the change from
    // Add which for key events
    if ( !event.which && (event.charCode || event.keyCode) )
    event.which = event.charCode || event.keyCode;
    to
    // Add which for key events
    if ( !event.which && ((event.charCode || event.charCode === 0) ? event.charCode : event.keyCode) ) {
    event.which = event.charCode || event.keyCode;
    }
    screwed something up – when using the left/right arrow keys, which would fall under the domain of event.keyCode, even.which is not getting set.

  • deadpoem

    I’m facing the same issue reported by Rigo in 1.4.

  • costinc

    Since 1.4 I’m getting this warning in my Firefox (3.6) error console on every keydown or keyup event:

    Warning: The ‘charCode’ property of a keydown event should not be used. The value is meaningless.

    It doesn’t affect functionality but the smallest warning in the error console tends to bug me.

  • Anonymous

    This isn’t really the fault of jQuery but try typing ‘;’, ‘=’, or ‘-’ in Firefox. Remember the values. OK, now check out the same characters on IE or Safari. ZOMG… different values 8-O. Look out for that.

  • Anonymous

    i am getting a similar issue.
    if u press 1 on the normal keypad it shows the value as 49 but if u press 1 on the numpad it shows it as 97, which shouldnt be the case.
    //=============================
    this is a normal javascript function which shows the keycode for 1 pressed at any of the places as 49,
    //=============================
    function is_int(event)
    {
    var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    alert(Key);
    }

    //=============================
    but if u use a Jquery script to check the keycode the result is something very strange…
    //=============================

    $(document).keydown(function (e) {
    alert(e.keyCode);
    });

  • Anonymous

    try:
    $(document).keydown(function (e) {
    alert(e.which);
    });

    jQuery normalizes keycode and charcode for you using .which

  • Bob

    It says you can use event.which to track button events as well, but when I use it, it only responds to left-clicks. If I left-click, event.which is 1, but if I right-click, nothing happens. How am I suppose to tell which button was pressed?

  • Jacutru

    Change
    (event.charCode || event.charCode === 0)
    to
    (event.charCode || event.charCode !== 0)

    seems to do the trick for me

  • http://jakob.dalsgaard.myopenid.com/ Jakob

    I found the same annoying behaviour using jQuery 1.4.2 with Firefox 3.6.3 as well. The fix for me was to remove “charCode” from the list of cloned attributes in line 1956 of jquery-1.4.2.js — this list contains attributes to be cloned from the original event in the browser to a tailored jQuery event object that is more sane.
    Reading the “charcode” attribute in the cloning procedure seemed to trigger the warning in the error console. Cloning of the actual attribute, be it “keyCode” or “charCode” seems to be handled fine if lines 1996-1998 are changed to:

    if ( !event.which && ((originalEvent.charCode || originalEvent.charCode === 0) ? event.charCode : event.keyCode) ) {
    event.which = originalEvent.charCode || event.keyCode;
    }

    i.e. a more delicate cloning of the charCode property if present.

  • http://www.learningjquery.com/ Karl Swedberg

    Thanks for hunting this down. It would be very helpful if you could post your findings on the bug tracker (dev.jquery.com) or the developing jquery forum at forum.jquery.com.

  • http://www.spiders-design.co.uk Daniel Chatfield

    That is interesting – it seems that it is firefox hat is the odd one out with chrome internet explorer and safari consistent. This is very annoying for me as I have to ode seperate functions for firefox.

  • patrick

    The return type for event.which states String, when it actuality it seems to return a Number.

  • Hasansamilbozdemir

    when I press numpad keys(0..9) this example gives only the code is 144.What is wrong?

  • Anonymous

    It seems that event.which is 1, 2, 3 for left, middle, right mouse button respectively. If that is always the case, could it be added to the documentation?

  • Anonymous

    It seems that event.which is 1, 2, 3 for left, middle, right mouse button respectively. If that is always the case, could it be added to the documentation?

  • Mbu725

    When it's used with the keypress event, i doesn't return de same values as with keyup or keydown.
    E.g. I press * :
    - keypress -> 42
    - keydown -> 106 or 56 (it depends if you type it from the numpad or with Shift+8)

  • Andreas Ringlstetter

    event.which seems to be allways “undefined” in internet explorer 9.