which property (event)
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 set for the onmousedown and onmouseup events in all browsers except in Internet Explorer before version 9. In Google Chrome and 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 before version 9, 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, Google Chrome and Safari, but its possible values are different from those in Internet Explorer (see the button property for details). If you need to detect the pressed mouse buttons, the simplest solution is to use the which property where it is supported and the button property in other cases. See Example 3 for details.
You can find the related objects in the Supported by objects section below.
This property is read-only.
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:
Example HTML code 2:
This example shows how to create a text field that does not accept numeric input:
Example HTML code 3:
This example illustrates the use of the button and which properties:
User Contributed Comments