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

charCode Event Attribute

charCode Event Attribute

Event Object Reference Event Object

Example

Get the Unicode value of the pressed keyboard key:

var

x = event.charCode;

x The output is:

119 // 119 is the letter "w"

Try it »

The bottom of this article contains more examples.


Definition and usage

The charCode attribute returnsonkeypressthe character code of the key value that triggered the event.

The Unicode character code is a number for a letter (for example, the number "97" represents the letter "a").

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: If this attribute is used for onkeydown or onkeyup event, the return value is always "0".

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.

**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.charCode || event.keyCode; // use

charCode

or

keyCode

, so that different browsers can be supported

Tip: You can also use the keyCode property to detect special keys (such as "caps lock" or arrow keys). The keyCode and charCode 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
charCode Yes 9.0 Yes Yes Yes

Syntax

event

.charCode

Technical Details

Return value: A number representing the Unicode character code
DOM version: DOM Level 2 Events

More examples

Example

Browser-compatible solution for getting the Unicode value of a key:

// If the browser does not support charCode, use keyCode (IE8 and earlier versions)

var

x = event.charCode || event.keyCode;

Try it »

Example

When the user presses the "O" key, a prompt message pops up:

function

myFunction(event) {

   

var

x = event.charCode || event.keyCode;

   

if

(x ==

111

|| x ==

79

) {

// o is 111, O is 79

        alert(

You pressed the 'O' key!

);

    }

}

Try it »

Example

Convert the Unicode value to a character:

var

x = event.charCode || evt.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