event.keyCode

  • Revision slug: DOM/event.keyCode
  • Revision title: event.keyCode
  • Revision id: 298623
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{DomRef()}}

Summary

Returns the Unicode value of a non-character key in a {{domxref("element.onkeypress","keypress")}} event or any key in any other type of keyboard event.

For constants equivalent to these numeric codes, see {{domxref("KeyboardEvent","KeyEvent")}}.

Syntax

event.keyCode

Examples

var value = event.keyCode;
  • value is the Unicode value of the key that was pressed.
<!DOCTYPE html>
<html lang="en">
<head>
<title>keyCode example</title>

<script type="text/javascript">

function showKeyCode(e) {
	alert( "keyCode for the key pressed: " + e.keyCode + "\n" );
}

</script>
</head>

<body onkeydown="showKeyCode(event);">
<p>Press any key.</p>
</body>
</html>

Notes

In a {{domxref("element.onkeypress","keypress")}} event, the Unicode value of the key pressed is stored in either the keyCode or {{domxref("event.charCode","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.

keyCode is always set in the {{domxref("element.onkeydown","keydown")}} and {{domxref("element.onkeyup","keyup")}} events. In these cases, charCode is never set.

To get the code of the key regardless of whether it was stored in keyCode or charCode, query the {{domxref("event.which","which")}} property.

Characters entered through an IME do not register through keyCode or charCode.

For a list of the keyCode values associated with particular keys, run the example in Example 7: Displaying Event Object Constants and view the resulting HTML table.

Specification

Not part of W3C DOM specification. See {{interface("nsIDOMKeyEvent")}}

Revision Source

<div>{{DomRef()}}</div>


<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns the Unicode value of a non-character key in a {{domxref("element.onkeypress","keypress")}} event or any key in any other type of keyboard event.</p>
<p>For constants equivalent to these numeric codes, see {{domxref("KeyboardEvent","KeyEvent")}}.</p>


<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre><code>event.keyCode</code></pre>


<h2 id="Examples" name="Examples">Examples</h2>
<pre><code>var <var>value</var> = event.keyCode;</code></pre>

<ul>
  <li><code>value</code> is the Unicode value of the key that was pressed.</li>
</ul>

<pre class="brush:xml">&lt;!DOCTYPE html>
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;title&gt;keyCode example&lt;/title&gt;

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

function showKeyCode(e) {
	alert( "keyCode for the key pressed: " + e.keyCode + "\n" );
}

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

&lt;body onkeydown="showKeyCode(event);"&gt;
&lt;p&gt;Press any key.&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>


<h2 id="Notes" name="Notes">Notes</h2>
<p>In a {{domxref("element.onkeypress","keypress")}} event, the Unicode value of the key pressed is stored in either the <code>keyCode</code> or {{domxref("event.charCode","charCode")}} 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>keyCode</code> is always set in the {{domxref("element.onkeydown","keydown")}} and {{domxref("element.onkeyup","keyup")}} events. In these cases, <code>charCode</code> is never set.</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 {{domxref("event.which","which")}} property.</p>
<p>Characters entered through an IME do not register through <code>keyCode</code> or <code>charCode</code>. <!--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.--></p>

<p>For a list of the <code>keyCode</code> values associated with particular keys, run the example in <a href="/en-US/docs/Gecko_DOM_Reference/Examples#Example_7:_Displaying_Event_Object_Constants" title="en-US/docs/Gecko_DOM_Reference/Examples#Example_7:_Displaying_Event_Object_Constants">Example 7: Displaying Event Object Constants</a> and view the resulting HTML table.</p>


<h2 id="Specification" name="Specification">Specification</h2>
<p>Not part of W3C DOM specification. See {{interface("nsIDOMKeyEvent")}}</p>
Revert to this revision