Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • 2
    Thanks T.J. Where would I find a reference for this? Not that I don't believe you, I'm just curious! ...I see you just added that, thanks. So, even for those not using jquery, .which is a better choice? Commented Dec 17, 2010 at 14:55
  • 7
    @ScottE: If not using jQuery, you have to handle this explicitly yourself. I usually do it like this var key = event.which || event.keyCode; That will use event.which if it's defined and not falsey, or event.keyCode if which is undefined or falsey. Technically I should probably do var key = typeof event.which === "undefined" ? event.keyCode : event.which; but if event.which is 0 (can it be 0?), I'm unlikely to care for the kinds of things I do. Commented Dec 17, 2010 at 15:02
  • 2
    @ScottE, here is a basic reference: quirksmode.org/js/keys.html (it doesn't include which, which I think is only provided by jQuery but I'm not 100% sure, but it should get you started on seeing differences in browsers) Commented Dec 17, 2010 at 15:05
  • 1
    @fudgey: which is provided for keypress by all browsers except IE. And quirksmode is not authoritative here. As a reference, the link that @T.J. Crowder posted is much better: unixpapa.com/js/key.html. Commented Dec 18, 2010 at 12:11
  • 5
    @T.J. Crowder: Yes, the event's which property can be zero, and this can make a big difference to most applications. For example, non-printable keys in Firefox have a which property of zero and the same keyCode property as keydown. The Home key has a keyCode of 36 on my PC in Firefox, which is the character code for "$", which would make it impossible to distinguish between the user pressing the Home key and the user typing a $ character using event.which || event.keyCode. Commented Dec 18, 2010 at 12:31