What is the functionality of Javascript event e.which
? Please brief with example.

- 5,753
- 72
- 57
- 129

- 6,895
- 16
- 48
- 55
-
45Two years later, I was having trouble understanding this property after reading several pages elsewhere. Then I stumbled across this old post and got it. So to @Reigel, maybe reconsider before telling someone to Google something. Maybe they already did. That is not SO's purpose. – temporary_user_name Apr 11 '12 at 02:27
-
@Aerovistae, on that time, searching google would give you something like http://www.quirksmode.org/js/events_properties.html ... which is very helpful already... – Reigel Gallarde Apr 11 '12 at 03:25
-
Tim Down's answer is so much superior than the accepted answer at the moment. – Toskan Sep 26 '12 at 13:46
-
5@JuanMendes - Googling "javascript event.which" got me here as the first result. Is that something you feel is "undesireable"? I mean, it sounds like you would rather that the question was never asked/answered here. – Kevin Fegan Feb 17 '14 at 19:01
4 Answers
which
is a property of Event
objects. It is defined for key-related and mouse-related events in most browsers, but in both cases is not defined in IE (prior to version 9).
For mouse-related events, which
specifies the mouse button that was involved. For IE < 9, the equivalent value is found in window.event.button
. Just to complicate things, non-IE browsers also support a button
property of mouse events that sometimes reports a different value from which
. Also, browsers sometimes have different values for the same button or combination of buttons. If you stick to using which
in all browsers that support it and button
in IE < 9, the one constant is that a value of 1 always means the left mouse button was involved (though not necessarily alone).
document.onmousedown = function(e) {
e = e || window.event;
var button = (typeof e.which != "undefined") ? e.which : e.button;
if (button == 1) {
alert("Left mouse button down");
}
};
For a full analysis, I recommend Jan Wolter's article on JavaScript mouse events.
For key-related events, which
relates to the key that has been pressed. For keydown
and keyup
events, this is relatively simple: it's the key code for the key pressed, and returns the same value as the event's keyCode
property. Since all browsers support the keyCode
property and IE < 9 does not support which
, you should generally use keyCode
for keydown
and keyup
events.
For keypress
events, the situation is more complicated. For printable character keys, which
is the character code for the key pressed and is supported in more browsers than the charCode
property. In IE < 9 the equivalent is again the keyCode
property. So for detecting the character typed, the following is a cross-browser approach. Be aware that the code below should not be used for non-printable keys such as arrow keys, which you should instead detect in the keydown
event:
document.onkeypress = function(e) {
e = e || window.event;
var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
if (charCode) {
alert("Character typed: " + String.fromCharCode(charCode));
}
};
Again, for more details I recommend Jan Wolter's article on JavaScript key events
-
5@Toskan: The accepted answer was posted a few hours before mine, and may have been accepted before I posted mine. – Tim Down Sep 26 '12 at 17:28
-
3
e.which
is not an event, which
is a property of the event
object, which most people label as e
in their event handlers. It contains the key code of the key which was pressed to trigger the event (eg: keydown, keyup).
document.onkeypress = function(myEvent) { // doesn't have to be "e"
console.log(myEvent.which);
};
With that code, the console will print out the code of any key you press on the keyboard.
Deprecation notice (as of September 2020)
KeyboardEvent.which has been deprecated. Please look for alternatives, such as KeyboardEvent.key. Read the full API here.

- 4,032
- 3
- 37
- 58

- 537,072
- 198
- 649
- 721
-
1It does not really require Firebug, only some sort of console.log (which many more than Firebug provides, for example the webkitt-ens). – npup Jun 16 '10 at 07:19
-
1`which` also exists on mouse events, and your example won't work on IE, in which an event handler does not receive an event parameter. – Tim Down Jun 16 '10 at 08:45
-
3Just another note of help for all who find this (will post on other e.which questions prolly too). I made a post at jQuery forums that list a good majority of e.which codes and their associated keys. The post is here -> http://forum.jquery.com/topic/eventwhich-code-list-just-for-help – SpYk3HH Jan 13 '12 at 17:01
-
[KeyboardEvent.which](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/which) has been deprecated. Please look for the alternatives, such as [KeyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key). See more [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent). – crimson_king Sep 28 '20 at 23:44
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.
You should use KeyboardEvent.key
instead, if it's available.

- 699
- 1
- 8
- 25
-
4`KeyboardEvent.key` will return **"the identifier of the key (char)"** while `KeyboardEvent.which` will return **"the numeric keyCode"**. – csr-nontol May 02 '19 at 23:17
-
1Note that [IE11 and Edge18 don't consistently implement the spec](https://caniuse.com/#feat=keyboardevent-key). – Ian Dunn May 10 '19 at 12:30
During an event, e
:
e.which
is same as:
e.keyCode
So both functions allow you to obtain the keycode of the key pressed during a keypress, keydown or keyup event
Many people use ||
(OR) to make sure their code works in browsers which don't support which property. Look at the code below:
document.onkeypress = function(e) {
var key = e.which || e.keyCode;
alert(key);
}

- 15,156
- 21
- 98
- 207

- 852
- 10
- 19