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

keyCode event attribute

keyCode event attribute

Event Object Reference Event Object

Example

Get the Unicode value of the pressed keyboard key:

var

x = event.keyCode;

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 keyCode 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: In Firefox, the keyCode property is invalid in the onkeypress event (returns 0). For browser compatibility issues, you can use the and keyCode properties together to solve it: which and the keyCode property to solve it:

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

property
keyCode Yes Yes Yes Yes Yes

Syntax

event

.keyCode

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;

    document.getElementById(

"demo"

).innerHTML =

"Unicode CHARACTER code: "

}

function

uniKeyCode(event) {

   

var

key = event.keyCode;

    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.keyCode;

   

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.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: which event attribute

Event Object Reference Event Object