jQuery event.which

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Event object · jQuery 1.1.3+

What You’ll Learn

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

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.

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).
  • Mouse events — on mousedown / mouseup, 1 left, 2 middle, 3 right.
  • 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.

📝 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.

Official jQuery API examples

jQuery
$( "#whichkey" ).on( "keydown", function ( event ) {
  $( "#log" ).html( event.type + ": " + event.which );
});
jQuery
$( "#whichkey" ).on( "mousedown", function ( event ) {
  $( "#log" ).html( event.type + ": " + event.which );
});

⚡ Quick Reference

Eventevent.whichMeaning
keydown Enter13Enter / Return key
keydown Escape27Escape key
keydown letter A65Uppercase A (US layout)
mousedown left1Left mouse button
mousedown middle2Middle mouse button
mousedown right3Right mouse button

📋 event.which vs keyCode vs charCode vs button

Four related legacy fields — jQuery’s which unifies keyboard and mouse input into one Number.

event.which
event.which

jQuery recommended — normalized key or button Number

event.keyCode
event.keyCode

Legacy physical key code on keydown/keyup — which merges this

event.charCode
event.charCode

Legacy character code — which normalizes with keyCode

event.button
event.button

Raw mouse button bitmask — prefer which (1/2/3) on mousedown/up

Examples Gallery

Example 1 follows the official keydown demo. Examples 2–5 cover mousedown buttons, Enter submit, Escape close, and which vs keyCode normalization.

📚 Official jQuery Demos

Log event.type and event.which on keyboard and mouse input.

Example 1 — Official Demo: keydown on #whichkey

Official jQuery pattern — bind keydown on an input; log event.type + ": " + event.which to #log.

jQuery
$( "#whichkey" ).on( "keydown", function ( event ) {
  $( "#log" ).html( event.type + ": " + event.which );
});
Try It Yourself

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.

jQuery
$( "#mousedown-area" ).on( "mousedown", function ( event ) {
  $( "#log" ).html( event.type + ": " + event.which );
});
Try It Yourself

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.

jQuery
$( "#search-form" ).on( "keydown", "#query", function ( event ) {
  if ( event.which === 13 ) {
    event.preventDefault();
    $( "#search-form" ).trigger( "submit" );
    $( "#status" ).text( "Submitted (Enter / which 13)" );
  }
});
Try It Yourself

How It Works

Enter maps to 13 across browsers in jQuery’s normalized which. Compare the Number in keydown — no need to read keyCode separately.

Example 4 — Escape key: close panel when event.which === 27

Bind keydown on document — hide a modal panel when the user presses Escape (which === 27).

jQuery
$( document ).on( "keydown", function ( event ) {
  if ( event.which === 27 && $( "#panel" ).is( ":visible" ) ) {
    $( "#panel" ).hide();
    $( "#log" ).text( "Panel closed (Escape / which 27)" );
  }
});
Try It Yourself

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.

jQuery
$( "#whichkey" ).on( "keydown", function ( event ) {
  $( "#log" ).html(
    "which: " + event.which +
    " | keyCode: " + event.keyCode +
    " | charCode: " + ( event.charCode || 0 )
  );
});
Try It Yourself

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.

🚀 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.

📝 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
which Number

Bottom line: Read event.which for keys and mouse buttons — 1 left, 2 middle, 3 right.

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.

💡 Best Practices

✅ Do

  • 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)

Key Takeaways

Knowledge Unlocked

Five things to remember about event.which

One Number for keys and mouse buttons.

5
Core concepts
1.1 02

Since 1.1.3

Normalize keys

History
demo 03

Official

keydown log

Demo
123 04

Mouse

1 / 2 / 3

Buttons
kc 05

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.

Next: event.namespace

Learn namespaced binds and read event.namespace in handlers.

event.namespace tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful