YouTip LogoYouTip

Event Key Which

[![Image 1: Event Object Reference](#) Event Object](#) ## Example Get the Unicode value of the pressed keyboard key: var x = event.which; _x_ output: 119 // 119 is the character "w" [Try it Β»](#) More examples are included at the bottom of this page. * * * ## Definition and Usage The `which` property returns the character code of the key that triggered the (#) event, or the key code of the key that triggered the (#) or (#) event. The difference between the two code types is: * Character code - A number representing an ASCII character * Keyboard code - A number representing a physical key on the keyboard The values of the two types are not always equal. For example, the lowercase character "w" and the uppercase character "W" have the same keyboard code because they are on the same key (the "W" key code is "87"), but they have different character codes, and the output for the two characters is different ("w" and "W" character codes are "119" and "87" respectively) - see the example below for a better understanding. **Tip:** If you need to know if the user pressed a printable key (like "a" or "5"), it is recommended to use the `onkeypress` event. If you need to know if the user pressed a function key (like "F1", "CAPS LOCK", or "Home"), you can use the `onkeydown` or `onkeyup` events. **Note:** The `which` property is not supported in IE8 and earlier. For browsers that do not support it, you can use the (#) property. However, the `keyCode` property does not work in the `onkeypress` event in Firefox. To be compatible with these browsers, you can use the following code: var x = event.which || event.keyCode; // Use **which** or **keyCode** to support different browsers **Tip:** You can view a list of all Unicode characters in our (#). **Tip:** If you need to convert a Unicode value to a character, you can use the [fromCharCode()](#) method. **Note:** This property is read-only. **Note:** The `which` and `keyCode` properties provide a way to solve browser compatibility issues. The latest version of the DOM Events specification recommends using the (#) property as an alternative. **Tip:** If you want to check if the "ALT", "CTRL", "META", or "SHIFT" key was pressed, you can use the (#), (#), (#), or (#) properties. * * * ## Browser Support The numbers in the table indicate the first browser version that supports the property. | Property | | | | | | | --- | --- | --- | --- | --- | --- | | which | Yes | 9.0 | Yes | Yes | Yes | * * * ## Syntax _event_.which ## Technical Details | Return Value: | A number representing the Unicode character code or Unicode key code | | --- | | DOM Version: | DOM Level 2 Events | * * * ![Image 2: Example](#) ## More Examples ## Example Demonstrate the difference between character code and keyboard code using `onkeypress` and `onkeydown`: function uniCharCode(event) { var char = event.which || event.keyCode; // event.keyCode is for IE8 and earlier document.getElementById("demo").innerHTML = "Unicode CHARACTER code: " + char; } function uniKeyCode(event) { var key = event.which || event.keyCode; // event.keyCode is for IE8 and earlier document.getElementById("demo2").innerHTML = "Unicode KEY code: " + key; } When the "a" key is pressed on the keyboard (without Caps Lock), the output is: Unicode character code: 97 Unicode keyboard code: 65 [Try it Β»](#) ## Example If the Esc key is pressed, show an alert message: function myFunction(event) { var x = event.which || event.keyCode; // event.keyCode is for IE8 and earlier if (x == 27) {// 27 is the ESC key alert ("You pressed the Escape key!"); } } [Try it Β»](#) ## Example Convert a Unicode value to a character (does not work for function keys): var x = event.which || event.keyCode;//Get Unicode value var y = String.fromCharCode(x);// Convert value to character [Try it Β»](#) * * * ## Related Pages HTML DOM Reference: (#) HTML DOM Reference: (#) HTML DOM Reference: (#) [![Image 3: Event Object Reference](#) Event Object](#)
← C Function RenameC Function Feof β†’