which property (event)
| A A | Font size |
|
|
Share |
|
Returns the Unicode character or key code of the key or the identifier of the mouse button that was pressed when the current event fired.
Key events:
Mouse events:
- In case of onkeypress events, the which property retrieves the Unicode character code of the key that was pressed.
- In case of onkeydown and onkeyup events, the which property retrieves the Unicode key code of the key that was pressed.
The which property is relevant for the onmousedown and onmouseup events in Firefox, Opera and Safari.
In Safari, the which property can also be used in case of onmousemove, onmouseout and onmouseover events, but the default value of this property is 1, so the left mouse button is not detectable in case of these events.
In Internet Explorer, use the button property instead.
If you need to detect whether the ALT, CONTROL, META or SHIFT key was down at the time when the event occurred, use the altKey, ctrlKey, metaKey or shiftKey property.
Note: the button property is also supported by Firefox, Opera and Safari, but its possible values are different from those in Internet Explorer (therefore, you need browser detection if you want to use the button property).
A better solution is to use the which property in Firefox, Opera and Safari and the button property in Internet Explorer.
See the second example for details.
Syntax:
You can find the related objects in the Supported by objects section below.
This property is read-only.
Possible values:
Integer that represents the Unicode character or key code or identifies the mouse button that was pressed.
Predefined constants are available for the possible Unicode key codes, in the scope of the KeyboardEvent interface. You can use them directly through the KeyboardEvent interface (KeyboardEvent.DOM_VK_A) or through an instance of the event object. For further details, please see the page for the KeyboardEvent interface. Unfortunately, these constants are only supported by Firefox, so use the numeric values instead of the predefined constants for a cross-browser solution.
For mouse buttons, the value of 1 means the left button, the value of 2 means the middle button and the value of 3 means the right button.Default: this property has no default value.
Example HTML code 1:
This example illustrates the use of the which property:
|
|
||||
<head> <script> function GetChar (event){ var keyCode = event.which; if (keyCode == undefined) { keyCode = event.keyCode; } alert ("The Unicode key code is: " + keyCode); } </script> </head> <body> <input size="40" value="Write a character into this field!" onkeydown="GetChar (event);"/> </body> |
||||
|
||||
|
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates the use of the button and which properties:
|
|
||||
<head> <script> function WhichButton (event) { if (event.which === undefined) { // Internet Explorer var buttons = ""; if (event.button & 1) { buttons += "left"; } if (event.button & 2) { if (buttons == "") { buttons += "right"; } else { buttons += " + right"; } } if (event.button & 4) { if (buttons == "") { buttons += "middle"; } else { buttons += " + middle"; } } alert ("The following buttons are pressed: " + buttons); } else { // Firefox, Opera and Safari switch (event.which) { case 1: alert ("Left button is pressed"); break; case 2: alert ("Middle button is pressed"); break; case 3: alert ("Right button is pressed"); break; } } } </script> </head> <body> <div onmousedown="WhichButton (event);">Press a mouse button over this text!</div> </body> |
||||
|
||||
|
Did you find this example helpful?
|
Supported by objects:
Related pages:
External links:
User Contributed Comments

