Code has been added to clipboard!

jQuery event.which: Track Button or Key Presses That Trigger Events

Reading time 2 min
Published Jan 8, 2018
Updated Oct 10, 2019

jQuery event.which: Main Tips

  • The event.which (shortened as e.which) property returns the Unicode value of a pressed key or button that triggered an event.
  • e.which stabilizes mousedown and mouseup events.

Usage of event.which

The event.which property was first introduced in the Netscape browser. It indicates which button of the keyboard or the mouse invoked the event.

In jQuery, event.which represents the which property for an event object. It's rather common to label the event object as e, therefore e.which is also available. As it is a property and not a function, the syntax does not require specifying any parameters:

Example
$("button").keydown((event) => {
    $("p").html("The key pressed down was: " + event.which);
});

In the example above, the Unicode value of the last key you press when typing into the input field will be displayed.

Note: the mouse button values when using e.which are as follows: a right-click is 1, a middle (or mouse wheel) click is 2, and a left-click is 3.

Most Common Character Codes

In the table below, you will find the most common Unicode character codes that jQuery event.which can return. Feel free to check the less common symbols in this guide.

Arrows

Code Meaning
37 Arrow pointing left
38 Arrow pointing up
39 Arrow pointing right
40 Arrow pointing down

Control Keys

Code Meaning
16 Shift
17 Ctrl
18 Alt
20 Caps Lock
8 Backspace
9 Tab
32 Space

Other Characters

Code Meaning
65–90 Letters of the alphabet (A–Z)
48–57 Numbers on the regular keyboard (0–9)
96–105 Numbers on the numeric keypad (0–9)
112–123 Function keys (F1–F12)
Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX