Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

which event attribute

which event attribute

Event Object Reference Event Object

Example

Get the Unicode value of the pressed keyboard key:

var

x = event.which;

x The output result is:

119 // 119 is the character "w"

Try it »

The bottom of this article contains more examples.


Definition and usage

The which property returnsonkeypressthe character code of the value of the key that triggered the event, or onkeydown or onkeyup the key code of the event.

The difference between the two code types is:

The values of the two types are not always equal. For example, the lowercase character "w" and the uppercase character "W" have the same key code because they are on the same key on the keyboard ("W" code is "87"), but they have different character codes, and the two characters output differently ("w" and "W" character codes are "119" and "87") - see the following example for a better understanding.

Tip: If you need to know whether the user pressed a printable 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. For browsers that do not support it, you can use keyCode the property. However, the keyCode property is invalid in the onkeypress event of Firefox. To be compatible with these browsers, you can use the following code:

var x = event.which || event.keyCode; // use

which

or

keyCode

, so that different browsers can be supported

Tip: A list of all Unicode characters can be found in our Complete Unicode Reference Manual.

Tip: If you need to convert a Unicode value to a character, you can use 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 DOM events recommends using key the property to replace this method.

Tip: If you want to check whether the "ALT", "CTRL", "META" or "SHIFT" keys are pressed, you can use the property. altKey, ctrlKey, metaKey or shiftKey attribute.


Browser support

The numbers in the table indicate the version number of the first browser that supports this 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

More examples

Example

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

"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: "

}

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: "

}

When the "a" key is pressed on the keyboard (without using caps lock), the output is as follows:

Unicode character code: 97

Unicode key code: 65

Try it »

Example

If the Esc key is pressed, a prompt message pops up:

"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!"

);

    }

}

Try it »

Example

Convert the Unicode value to a character (cannot be used for function keys):

var

x = event.which || event.keyCode;  

// Get the Unicode value

var

y = String.fromCharCode(x);        

// Convert the value to a character

Try it »


Related pages

HTML DOM Reference Manual: key event attribute

HTML DOM Reference Manual: keyCode event attribute

HTML DOM Reference Manual: charCode Event Attribute

Event Object Reference Event Object