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.
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

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)

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 22/01/2021
3176 People Used
Only 97 Left
Rating
5.0