event.which returns a Number for the key or mouse button that fired — jQuery’s normalized alternative to keyCode, charCode, and button. This tutorial covers the official keydown and mousedown demos, Enter and Escape shortcuts, comparing with legacy properties, and five try-it labs.
01
Property
event.which
02
Returns
Number
03
Keyboard
Key pressed
04
Mouse
1 / 2 / 3
05
Normalize
keyCode
06
Since 1.1.3
Read-only
Fundamentals
Introduction
When you handle keyboard or mouse input in jQuery, you often need to know which key or button the user pressed. The event.which property answers that with a single Number — no juggling separate legacy fields.
jQuery has exposed event.which since version 1.1.3. For keyboard events it normalizes event.keyCode and event.charCode; the official docs recommend watching which for key input. For mousedown and mouseup, it reports 1 (left), 2 (middle), and 3 (right) — use it instead of event.button.
Concept
Understanding event.which
event.which indicates the specific key or button that was pressed:
Keyboard events — a numeric key code; compare to constants like 13 (Enter) or 27 (Escape).
Normalization — jQuery merges keyCode and charCode behavior into one property.
Read-only — reflects what the browser reported; you cannot reassign which key fired.
💡
Beginner Tip
Need the physical key on keydown? Read event.which. Need which mouse button on mousedown? Same property — 1, 2, or 3. Skip raw button and split keyCode/charCode logic.
Foundation
📝 Syntax
Official jQuery API form (since 1.1.3):
jQuery
event.which
// read inside any .on() handler:
function handler( event ) {
console.log( "Key or button:", event.which );
}
// keyboard — Enter key:
$( "#search" ).on( "keydown", function ( event ) {
if ( event.which === 13 ) { /* submit */ }
});
// mouse — left button only:
$( "#area" ).on( "mousedown", function ( event ) {
if ( event.which === 1 ) { /* left click */ }
});
Type
Number — key code for keyboard events; button index for mouse down/up.
Return value
Not a method — a read-only property on the event object passed to your handler.
Normalized by jQuery — recommended over keyCode, charCode, and button.
Type in #whichkey → #log shows "keydown: 65" (letter A)
Press Enter → "keydown: 13"
Each key updates type and which in real time
How It Works
On every keydown, jQuery sets event.which to the normalized key code. The official demo concatenates type and which — the simplest way to inspect keyboard input.
Example 2 — Official Demo: mousedown button 1, 2, or 3
Same pattern for mouse — bind mousedown; log which button: left 1, middle 2, right 3.
Left click → "mousedown: 1"
Middle click → "mousedown: 2"
Right click → "mousedown: 3"
jQuery normalizes button — no raw event.button math
How It Works
Browsers expose mouse buttons inconsistently via event.button. jQuery’s event.which maps them to 1, 2, and 3 on mousedown and mouseup — the official second demo.
📈 Practical Patterns
Enter submit, Escape dismiss, and legacy property comparison.
Example 3 — Enter key: submit form when event.which === 13
Intercept Enter in a search field — submit the parent form on keydown when event.which === 13.
Open #panel, press Escape → panel hides
#log → "Panel closed (Escape / which 27)"
Global keydown + which 27 is a standard dismiss pattern
How It Works
Escape is 27 in the same Number space as Enter. A document-level keydown listener plus event.which closes overlays without clicking — common in modal UIs.
Example 5 — which vs keyCode: log both on keydown
Side-by-side log of event.which and event.keyCode — show jQuery normalization on the same keypress.
Press A → "which: 65 | keyCode: 65 | charCode: 0"
Press Enter → "which: 13 | keyCode: 13 | charCode: 0"
which and keyCode usually match on keydown — prefer which per jQuery docs
How It Works
On keydown, which and keyCode often agree — jQuery built which to unify keyCode and charCode across event types. Use which in new code; log all three when debugging legacy handlers.
Applications
🚀 Common Use Cases
Keyboard shortcuts — compare event.which to 13, 27, or letter codes in keydown handlers.
Enter-to-submit — submit search or chat forms when which === 13 without a click.
Escape-to-close — dismiss modals and panels on which === 27.
Mouse button filtering — run logic only on left click (which === 1) on mousedown.
Context menu guard — ignore or handle right click when which === 3.
Legacy migration — replace split keyCode/charCode checks with a single event.which read.
🧠 How event.which Is Set
1
User input
The user presses a key or mouse button — the browser fires keydown, mousedown, or related events.
Input
2
Native event
The DOM event carries raw keyCode, charCode, or button values that vary by browser and event type.
Native
3
jQuery normalizes
jQuery wraps the native event and sets event.which — one Number for keys or buttons 1/2/3.
Normalize
4
✓
Read event.which
Your .on() handler compares the Number — Enter, Escape, or left/middle/right mouse.
Important
📝 Notes
Available since jQuery 1.1.3 — returns a Number for the key or button pressed.
Keyboard — normalizes event.keyCode and event.charCode; recommended for key input per official docs.
Mouse — on mousedown and mouseup, 1 left, 2 middle, 3 right — use instead of event.button.
Read-only property — jQuery sets it from the native event; do not reassign.
Not a function — access as event.which, never event.which().
Modern apps may also use event.key and event.code on native events — which remains the jQuery-documented Number API.
Compare to numeric constants (13, 27) in keydown — keypress-specific charCode branching is rarely needed with which.
Compatibility
Browser Support
event.which is set on jQuery’s normalized event object since jQuery 1.1.3+. Underlying keyboard and mouse events are supported across all browsers jQuery targets; which abstracts legacy keyCode/charCode and button differences.
✓ jQuery 1.1.3+ · all browsers
jQuery event.which
Normalized key or mouse button Number.
100%Universal
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
whichNumber
Bottom line: Read event.which for keys and mouse buttons — 1 left, 2 middle, 3 right.
Wrap Up
Conclusion
event.which gives you one Number for keyboard keys and mouse buttons since jQuery 1.1.3. The official demos log type and which on keydown and mousedown; practical handlers compare to 13 (Enter) or 27 (Escape).
Prefer event.which over raw keyCode, charCode, and button in jQuery code — it is the documented normalization layer for input events.
Read event.which for keyboard input in jQuery handlers
Use event.which === 1 to filter left-click on mousedown
Compare which === 13 or 27 for Enter and Escape UX
Log which alongside keyCode when debugging legacy code
Bind keydown on inputs or document for shortcut and dismiss patterns
❌ Don’t
Call event.which() with parentheses — it is a property
Rely on event.button when event.which is available
Split keyCode vs charCode logic when which already normalizes both
Assign to event.which — it is read-only
Expect which on events with no key or button semantics (e.g. pure custom triggers)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about event.which
One Number for keys and mouse buttons.
5
Core concepts
#01
event.which
Number property
API
1.102
Since 1.1.3
Normalize keys
History
demo03
Official
keydown log
Demo
12304
Mouse
1 / 2 / 3
Buttons
kc05
keyCode
Use which
Prefer
❓ Frequently Asked Questions
event.which is a Number property on the jQuery event object. For keyboard events it indicates which key was pressed; for mouse events on mousedown and mouseup it indicates which button was pressed. Available since jQuery 1.1.3.
Bind keydown on an input: $("#whichkey").on("keydown", function(event) { $("#log").html(event.type + ": " + event.which); }); The log shows the event type and numeric key code. The mousedown variant logs 1, 2, or 3 for left, middle, and right buttons.
jQuery recommends event.which for keyboard input. It normalizes event.keyCode and event.charCode into one Number — you get a consistent value without branching on event type. Prefer which over raw keyCode in jQuery handlers.
On mousedown and mouseup, event.which reports 1 for the left button, 2 for the middle button, and 3 for the right button. Use event.which instead of event.button — jQuery normalizes browser differences.
Compare event.which to well-known key codes in a keydown handler: event.which === 13 for Enter (submit a form), event.which === 27 for Escape (close a panel). Bind on the input or container and preventDefault when you handle the key yourself.
Not always. charCode reflects the Unicode character from keypress-style events; keyCode reflects the physical key. event.which merges both behaviors — on keydown it typically matches keyCode, and jQuery documents it as the recommended single property for key input.
Did you know?
jQuery introduced event.which in version 1.1.3 specifically to fix inconsistent keyCode and charCode behavior across browsers — years before modern KeyboardEvent.key became standard. The same property later normalized mouse button values to 1, 2, and 3, so one field covers both keyboard and mouse input in legacy jQuery apps.