Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Nos bénévoles n'ont pas encore traduit cet article en Français. Aidez-nous à réaliser cette tâche !

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

Deprecated
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.

The KeyboardEvent.charCode read-only property returns the Unicode value of a character key pressed during a keypress event.

For constants equivalent to these numeric codes, see KeyEvent.

Do not use this property anymore, it is deprecated.

Use KeyboardEvent.key instead.

Syntax

var value = event.charCode;
  • value is the Unicode value of the character key that was pressed.

Example

<html>
<head>
<title>charCode example</title>

<script type="text/javascript">

function showChar(e)
{
alert("Key Pressed: " + String.fromCharCode(e.charCode) + "\n"
      + "charCode: " + e.charCode);
}

</script>
</head>

<body onkeypress="showChar(event);">
<p>Press any 'character' type key.</p>
</body>
</html>

Notes

In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property, never both. If the key pressed generates a character (e.g. 'a'), charCode is set to the code of that character, respecting the letter case. (i.e. charCode takes into account whether the shift key is held down). Otherwise, the code of the pressed key is stored in keyCode.

When one or more modifier keys are pressed, there are some complex rules for charCode, see Gecko Keypress Event for the detail.

charCode is never set in the keydown and keyup events. In these cases, keyCode is set instead.

To get the code of the key regardless of whether it was stored in keyCode or charCode, query the which property.

Characters entered through an IME do not register through keyCode or charCode. Actually with the Chinese IME I'm using, entering the IME results in a keypress event with keyCode = 229 and no other key events fire until the IME exits (which may happen after multiple characters are inputted). I'm not sure if other IME's work this way.

For a list of the charCode values associated with particular keys, run the example in Gecko DOM Reference:Examples #Example 7: Displaying Event Object Constants and view the resulting HTML table.

Specifications

Specification Status Comment
Document Object Model (DOM) Level 3 Events Specification
The definition of 'KeyboardEvent.charCode' in that specification.
Working Draft Initial definition; specified as deprecated

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 26 (probably earlier) 3 9 12.1 5.1 (probably earlier)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? ? ? ? 5.1 (probably earlier)

Étiquettes et contributeurs liés au document

 Dernière mise à jour par : Mottie,