jQuery event.which: Main Tips
- The
event.which
(shortened ase.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:
$("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) |