event.charCode

« DOM Reference

Deprecated
This feature has been removed from the Web. 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.

Summary

Returns the Unicode value of a character key pressed during a keypress event.

For constants equivalent to these numeric codes, see KeyEvent.

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.

Specification

Not part of any specification. You should use char instead. See KeyEvent.

Document Tags and Contributors

Last updated by: kscarfone,