What is the functionality of Javascript event e.which
? Please brief with example.
The event. which is an inbuilt property in jQuery which is used to return which keyboard key or mouse button was pressed for the event.
JavaScript KeyCodeThe keydown event occurs when the keyboard key is pressed, and it is followed at once by the execution of keypress event. The keyup event is generated when the key is released.
There are three different keyboard events in JavaScript: keydown : Keydown happens when the key is pressed down, and auto repeats if the key is pressed down for long. keypress : This event is fired when an alphabetic, numeric, or punctuation key is pressed down. keyup : Keyup happens when the key is released.
KeyCode was deprecated because in practice it was “inconsistent across platforms and even the same implementation on different operating systems or using different localizations.” The new recommendation is to use key or code .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With