🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

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.
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
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
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

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)