A Number property that tells you which key or which mouse button was pressed. Added in jQuery 1.1.3. It normalizes keyCode / charCode for keys, and reports 1 / 2 / 3 for left / middle / right mouse buttons.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
event.which // Number property — no parentheses
// Official keydown idea
$("#whichkey").on("keydown", function (event) {
$("#log").html(event.type + ": " + event.which);
});
// Mouse: 1 = left, 2 = middle, 3 = right
// Keys: 13 = Enter, 27 = Escape (common codes)
Call
What it does
event.which
Learn jQuery event.which: read the normalized key or mouse button as a Number, official keydown and mousedown demos, Enter and Escape shortcuts, 1/2/3 for left/middle/right, examples, and FAQs.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Bind keydown (keys) or mousedown / mouseup (buttons).
Step 2
Accept the event argument in the handler.
Step 3
Read event.which (property — no ()).
Return Value
Return Value
Description
Value / undefined
Getters usually return a value from the first matched element, or undefined when nothing applies.
Prevent default. Stop unwanted form submit if needed.
Act. Read the input value and show a message.
Frequently Asked Questions
event.which is a Number property on the jQuery event object. For keyboard events it indicates which key was pressed; for mousedown and mouseup it indicates which button was pressed. Available since jQuery 1.1.3.
Bind keydown: $("#whichkey").on("keydown", function(event) { $("#log").html(event.type + ": " + event.which); }); The log shows the event type and numeric code. The mousedown variant logs 1, 2, or 3 for left, middle, and right.
jQuery recommends event.which for keyboard input. It normalizes event.keyCode and event.charCode into one Number. Prefer which over raw keyCode in jQuery handlers.
On mousedown and mouseup, event.which reports 1 for left, 2 for middle, and 3 for right. Use event.which instead of event.button — jQuery normalizes browser differences.
Compare event.which in a keydown handler: 13 for Enter, 27 for Escape. Bind on the input or container and call preventDefault when you handle the key yourself.
In the modern DOM, KeyboardEvent.which is deprecated in favor of event.key (and mouse buttons favor event.buttons / event.button). jQuery still documents event.which as the normalized Number for key and mouse in jQuery code. Prefer event.key for new non-jQuery code when you can.