KeyboardEvent which property
Definition and usage
which property returns the triggering onkeypress Event the Unicode character code of the key, or the onkeydown or onkeyup The Unicode key code of the key that triggered the event
The difference between the two types of codes:
- Character code - represents the numeric value of the ASCII character
- Key code - represents the numeric value of the actual key on the keyboard
These types do not always mean the same thing. For example, the keyboard code for lowercase "w" and uppercase "W" is the same, because the key pressed on the keyboard is the same ("W" = number "87"), but the character code is different, because the resulting character is different ("w" or "W", that is, "119" or "87") - please see more examples below for a better understanding of this point.
Tip:If you want to know if the user has pressed a printable key (such as "a" or "5"), it is recommended to use this property on the onkeypress event. To find out if the user has pressed a function key (such as "F1", "CAPS LOCK", or "Home"), please use the onkeydown or onkeyup event.
Note:IE8 and earlier versions do not support the which property. For these browser versions, you can use the keyCode property. However, the keyCode property does not work with the onkeypress event in Firefox. For a cross-browser solution, you can use the following code:
var x = event.which || event.keyCode; // Use which or keyCode depending on browser support
Tip:For a list of all Unicode characters, please refer to our Complete Unicode reference.
Tip:If you need to convert a Unicode value to a character, please use fromCharCode() method.
Note:This property is read-only.
Note:The keyCode and which properties are provided for compatibility reasons. The latest version of the DOM event specification recommends using the key property instead (if available).
Tip:If you want to know whether the "ALT", "CTRL", "META", or "SHIFT" keys were pressed at the time of the key event, use altKey,ctrlKey,metaKey or shiftKey Property.
Example
Get the Unicode value of the pressed keyboard key:
var x = event.which;
More TIY examples are located at the bottom of the page.
Syntax
event.which
Technical details
Return value: | Numeric value, representing the Unicode character code or Unicode keyboard code. |
---|---|
DOM version: | DOM Level 2 Events |
Browser support
The numbers in the table indicate the first browser version that fully supports this property.
Property | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
which | Supported | 9.0 | Supported | Supported | Supported |
More examples
Example
Demonstrate the difference between character codes and keyboard codes using onkeypress and onkeydown:
<input type="text" onkeypress="uniCharCode(event)" onkeydown="uniKeyCode(event)"> 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 pressing the "a" key on the keyboard (without using Capslock), the results of char and key will be:
Unicode CHARACTER code: 97 Unicode KEY code: 65
Example
Prompt some text if the user presses the Escape key:
<input type="text" onkeydown="myFunction(event)"> 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!"); } }
Example
Convert Unicode value to character (not applicable to function keys):
var x = event.which || event.keyCode; // Get Unicode value var y = String.fromCharCode(x); // Convert value to character
Related Pages
HTML DOM Reference Manual:KeyboardEvent key Property
HTML DOM Reference Manual:KeyboardEvent keyCode Property
HTML DOM Reference Manual:KeyboardEvent charCode Property