event.charCode

  • Revision slug: DOM/event.charCode
  • Revision title: event.charCode
  • Revision id: 105793
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment er, keyup, keydown, and keypress are not properties of Event object

Revision Content

{{ DomRef() }}

Summary

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

Syntax

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.

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 specification. See nsIDOMKeyEvent

{{ languages( { "pl": "pl/DOM/event.charCode" } ) }}

Revision Source

<p>
{{ DomRef() }}
</p>
<h3 name="Summary"> Summary</h3>
<p>Returns the Unicode value of a character key pressed during a <a href="en/DOM/element/keypress">keypress</a> event.
</p>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval"><i>value</i> = event.charCode
</pre>
<ul><li> <code>value</code> is the Unicode value of the character key that was pressed.
</li></ul>
<h3 name="Example"> Example </h3>
<pre>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;charCode example&lt;/title&gt;

&lt;script type="text/javascript"&gt;

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

&lt;/script&gt;
&lt;/head&gt;

&lt;body onkeypress="showChar(event);"&gt;
&lt;p&gt;Press any 'character' type key.&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h3 name="Notes"> Notes </h3>
<p>In a <a href="en/DOM/event/keypress">keypress</a> event, the Unicode value of the key pressed is stored in either the <code><a href="en/DOM/event.keyCode">keyCode</a></code> or <code>charCode</code> property, never both. If the key pressed generates a character (e.g. 'a'), <code>charCode</code> is set to the code of that character, respecting the letter case. (i.e. <code>charCode</code> takes into account whether the shift key is held down). Otherwise, the code of the pressed key is stored in <code>keyCode</code>.
</p><p><code>charCode</code> is never set in the <a href="en/DOM/event/keydown">keydown</a> and <a href="en/DOM/event/keyup">keyup</a> events. In these cases, <code>keyCode</code> is set instead.
</p><p>To get the code of the key regardless of whether it was stored in <code>keyCode</code> or <code>charCode</code>, query the <a href="en/DOM/event.which">which</a> property.
</p><p>Characters entered through an IME do not register through <code>keyCode</code> or <code>charCode</code>. <span class="comment">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.</span>
</p><p>For a list of the <code>charCode</code> values associated with particular keys, run the example in <a href="en/Gecko_DOM_Reference/Examples#Example_7:_Displaying_Event_Object_Constants">Gecko DOM Reference:Examples #Example 7: Displaying Event Object Constants</a> and view the resulting HTML table.
</p>
<h3 name="Specification">Specification </h3>
<p>Not part of specification. See <a href="en/NsIDOMKeyEvent">nsIDOMKeyEvent</a>
</p>{{ languages( { "pl": "pl/DOM/event.charCode" } ) }}
Revert to this revision