whichEvent Property
Examples
Get the Unicode value of the pressed keyboard key:
xThe output result is:
Try it yourself »
More examples are included at the bottom of this article.
Definition and Usage
The which property returnsonkeypressthe character code of the key that triggered the event, oronkeydownoronkeyupthe key code of the key.
The difference between the two types of codes is:
- Character code - a number representing an ASCII character
- Keyboard code - a number representing the actual key on the keyboard
Both types of values are not always equal. For example, the lowercase character "w" and the uppercase character "W" have the same keyboard code (the "W" code is "87"), but they have different character codes. The two characters produce different output (the character codes for "w" and "W" are "119" and "87") - see the examples below for a better understanding.
Tip:If you need to know whether the user pressed a printing key (such as "a" or "5"), it is recommended to use the onkeypress event. If you need to know whether the user pressed a function key (such as "F1", "CAPS LOCK" or "Home"), you can use the onkeydown or onkeyup event.
Note:IE8 and earlier versions do not support the which property. Browsers that do not support it can use thekeyCodeproperty. However, the keyCode property is invalid in the onkeypress event in Firefox. To be compatible with these browsers, you can use the following code:
Tip:For a list of all Unicode characters, see ourComplete Unicode Reference Manual。
Tip:If you need to convert a Unicode value to a character, you can use thefromCharCode()method.
Note:This property is read-only.
Note:The which and keyCode properties provide a method for browser compatibility. For the latest version of DOM events, it is recommended to use thekeyproperty to replace this method.
Tip:If you want to check whether the "ALT", "CTRL", "META" or "SHIFT" key is pressed, you can use thealtKey, ctrlKey, metaKeyorshiftKeyproperty.
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
| Property | |||||
|---|---|---|---|---|---|
| which | Yes | 9.0 | Yes | Yes | Yes |
Syntax
Technical Details
| Return Value: | A number, representing a Unicode character code or a Unicode key code |
|---|---|
| DOM Version: | DOM Level 2 Events |
More Examples
Examples
Use onkeypress and onkeydown to demonstrate the difference between character code and keyboard code:
function uniCharCode(event) {
var char = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions
document.getElementById("demo").innerHTML = "Unicode CHARACTER code: " + char;
}
function uniKeyCode(event) {
var key = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions
document.getElementById("demo2").innerHTML = "Unicode KEY code: " + key;
}
When the "a" key is pressed on the keyboard (without Caps Lock), the output is as follows:
Unicode keyboard code: 65
Try it yourself »
Examples
If the Esc key is pressed, pop up an alert message:
function myFunction(event) {
var x = event.which || event.keyCode; // event.keyCode is used for IE8 and earlier versions
if (x == 27) { // 27 is the ESC key
alert ("You pressed the Escape key!");
}
}
Try it yourself »
Examples
Convert a Unicode value to a character (cannot be used for function keys):
var y = String.fromCharCode(x); // Convert the value to a character
Try it yourself »
Related Pages
HTML DOM Reference:key Event Property
HTML DOM Reference:keyCode Event Property
HTML DOM Reference:charCode Event Property
Other Extensions
Event Object