Latest web development tutorials

which event attributes

Event Object Reference Event objects

Examples

Being Unicode values ​​by pressing the keys on the keyboard:

var x = event.which;

x The output is:

119 // 119 is the character "w"

try it"

Bottom of this article contains more examples.


Definitions and use

which property returns onkeypress event trigger key character code value or onkeydown or onkeyup event key code.

Is the difference between the two types of code:

  • Character code - Indicates an ASCII character
  • Keyboard codes - represents the number keys on the keyboard real

Two types of values ​​are not equal, for example, lowercase characters "w" and uppercase "W" have the same keyboard code because they are on their keyboard ( "W" code "87"), but they have different characters code, two characters are not the same output ( "w" and "W" character code "119" and "87") - See the following examples may be better understood.

Tip: If you need to know the user presses the print key (such as "a" or "5"), it is recommended to use onkeypress event. If you need to know the user presses a function key (such as "F1", "CAPS LOCK" or "Home") can be used onkeydown or onkeyup event.

Note: IE8 and earlier versions do not support which attributes. Unsupported browser can use keyCode property. However, keyCode properties in the Firefox browser onkeypress event is invalid. Compatible with these browsers you can use the following code:

var x = event.which || event.keyCode; // use which or keyCode, this may support different browsers

Note: A list of all Unicode characters can see our complete Unicode reference manual .

Tip: If you need to convert Unicode character value, you can use fromCharCode () method.

Note: This property is read-only.

Note: which keyCode property and provides a solution method for browser compatibility, the latest version of the DOM event recommended key attribute as an alternative method.

Tip: If you want to see if the press "ALT", "CTRL", "META" or "SHIFT" key, you can use altKey , ctrlKey , metakey or shiftKey property.


Browser Support

Figures in the table represent the first browser to support the property version number.

Attributes
which Yes 9.0 Yes Yes Yes


grammar

event .which

technical details

return value: Number that represents the Unicode character code or Unicode key code
DOM version: DOM Level 2 Events


Examples

More examples

Examples

Use onkeypress and onkeydown to demonstrate the difference between character code and keyboard codes:

<input type = "text" onkeypress = "uniCharCode (event)" onkeydown = "uniKeyCode (event)">

function uniCharCode (event) {
var char = event.which || event.keyCode; // event.keyCode for IE8 and earlier versions
document.getElementById ( "demo") .innerHTML = "Unicode CHARACTER code:" + char;
}

function uniKeyCode (event) {
var key = event.which || event.keyCode; // event.keyCode for IE8 and earlier versions
document.getElementById ( "demo2") .innerHTML = "Unicode KEY code:" + key;
}

When pressed on the keyboard "a" key (do not use Caps Lock), the output results are as follows:

Unicode character code: 97
Unicode keyboard codes: 65

try it"

Examples

If you press the Esc key to pop up a message:

<input type = "text" onkeydown = "myFunction (event)">

function myFunction (event) {
var x = event.which || event.keyCode; // event.keyCode for IE8 and earlier versions
if (x == 27) {// 27 ESC key is
alert ( "You pressed the Escape key !");
}
}

try it"

Examples

To convert Unicode values ​​for the characters (not available for the function keys):

var x = event.which || event.keyCode; // Gets the Unicode value
var y = String.fromCharCode (x); // convert the value to a character

try it"


Related Pages

HTML DOM Reference Manual: Key Event Properties

HTML DOM Reference Manual: the keyCode property event

HTML DOM Reference: charCode Event Properties

Event Object Reference Event objects