UIEvent: which property

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

The UIEvent.which read-only property of the UIEvent interface returns a number that indicates which button was pressed on the mouse, or the numeric keyCode or the character code (charCode) of the key pressed on the keyboard.

Value

Value for KeyboardEvent

For KeyboardEvent, event.which contains the numeric code for a particular key pressed, depending on whether an alphanumeric or non-alphanumeric key was pressed. Please see deprecated KeyboardEvent.charCode and KeyboardEvent.keyCode for more details.

Note: Consider KeyboardEvent.key or KeyboardEvent.code for new code.

Value for MouseEvent

For MouseEvent, event.which is a number representing a given button:

  • 0: No button
  • 1: Left button
  • 2: Middle button (if present)
  • 3: Right button

For a mouse configured for left-handed use, the button actions are reversed. In this case, the values are read from right to left.

Note: Consider MouseEvent.button for new code.

Examples

html
<p>Please press any key.</p>
js
function showKeyPress(evt) {
  console.log(
    `onkeypress handler:\n` +
      `keyCode property: ${evt.keyCode}\n` +
      `which property: ${evt.which}\n` +
      `charCode property: ${evt.charCode}\n` +
      `Character Key Pressed: ${String.fromCharCode(evt.charCode)}\n`,
  );
}

function keyDown(evt) {
  console.log(
    `onkeydown handler:\n` +
      `keyCode property: ${evt.keyCode}\n` +
      `which property: ${evt.which}\n`,
  );
}

document.addEventListener("keypress", showKeyPress);
document.addEventListener("keydown", keyDown);

Specifications

Specification
UI Events
# dom-uievent-which

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
which
Deprecated
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote
footnote When selecting an autocomplete suggestion, a keydown event fires where the event's which property is undefined. See bug 41425904.
Edge – Partial support
Edge 12 – 18 (Release date: 2015-07-29)
footnote Partial support
footnote Only supported for KeyboardEvent and MouseEvent, not all UIEvent objects.
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote
footnote On mousemove events, the which property is incorrectly always set to 1.
Opera – Partial support
Opera 12.1 – 12.1 (Release date: 2012-11-20)
footnote Partial support
footnote Only supported for KeyboardEvent and MouseEvent, not all UIEvent objects.
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 1 (Release date: 2003-06-23)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote
footnote When selecting an autocomplete suggestion, a keydown event fires where the event's which property is undefined. See bug 41425904.
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote
footnote On mousemove events, the which property is incorrectly always set to 1.
Opera Android – Partial support
Opera Android 12.1 – 12.1 (Release date: 2012-10-09)
footnote Partial support
footnote Only supported for KeyboardEvent and MouseEvent, not all UIEvent objects.
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote
footnote When selecting an autocomplete suggestion, a keydown event fires where the event's which property is undefined. See bug 41425904.
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote
footnote When selecting an autocomplete suggestion, a keydown event fires where the event's which property is undefined. See bug 41425904.
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Deprecated. Not for use in new websites.
See implementation notes.
Has more compatibility info.

See also