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

Revision 1109699 of KeyboardEvent.which

  • Revision slug: Web/API/KeyboardEvent/which
  • Revision title: KeyboardEvent.which
  • Revision id: 1109699
  • Created:
  • Creator: jwhitlock
  • Is current revision? Yes
  • Comment Remove width set in style

Revision Content

{{ APIRef("DOM Events") }}{{Deprecated_header}}

The KeyboardEvent.which read-only property returns the numeric keyCode of the key pressed, or the character code (charCode) for an alphanumeric key pressed.

Syntax

var keyResult = event.which;
  • keyResult contains the numeric code for a particular key pressed, depending on whether an alphanumeric or non-alphanumeric key was pressed. Please see {{domxref("KeyboardEvent.charCode")}} and {{domxref("KeyboardEvent.keyCode")}} for more details.

Example

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

<script type="text/javascript">

function showKeyPress(evt)
{
alert("onkeypress handler: \n"
      + "keyCode property: " + evt.keyCode + "\n"
      + "which property: " + evt.which + "\n"
      + "charCode property: " + evt.charCode + "\n"
      + "Character Key Pressed: "
      + String.fromCharCode(evt.charCode) + "\n"
     );
}


function keyDown(evt)
{
alert("onkeydown handler: \n"
      + "keyCode property: " + evt.keyCode + "\n"
      + "which property: " + evt.which + "\n"
     );
}


</script>
</head>

<body
 onkeypress="showKeyPress(event);"
 onkeydown="keyDown(event);"
>

<p>Please press any key.</p>

</body>
</html>

Specifications

Specification Status Comment
{{SpecName('DOM3 Events','#widl-KeyboardEvent-which','KeyboardEvent.which')}} {{Spec2('DOM3 Events')}} Initial definition; specified as deprecated

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4 2 [1] 9 10.10 5.1
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown }}[1] 10 {{ CompatUnknown() }} 5.1

[1] Gecko implements this property on the {{domxref("UIEvent")}} interface.

See also

  • {{domxref("KeyboardEvent")}}, the interface this property belongs too.

Revision Source

<p>{{ APIRef("DOM Events") }}{{Deprecated_header}}</p>

<p>The <code><strong>KeyboardEvent.which</strong></code> read-only property returns the numeric <code>keyCode</code> of the key pressed, or the character code (<code>charCode</code>) for an alphanumeric key pressed.</p>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox">
<em>var keyResult</em> = event.which;
</pre>

<ul>
 <li><em><code>keyResult</code></em> contains the numeric code for a particular key pressed, depending on whether an alphanumeric or non-alphanumeric key was pressed. Please see {{domxref("KeyboardEvent.charCode")}} and {{domxref("KeyboardEvent.keyCode")}} for more details.</li>
</ul>

<h2 id="Example" name="Example">Example</h2>

<pre class="brush: html">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;charCode/keyCode/which example&lt;/title&gt;

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

function showKeyPress(evt)
{
alert("onkeypress handler: \n"
      + "keyCode property: " + evt.keyCode + "\n"
      + "which property: " + evt.which + "\n"
      + "charCode property: " + evt.charCode + "\n"
      + "Character Key Pressed: "
      + String.fromCharCode(evt.charCode) + "\n"
     );
}


function keyDown(evt)
{
alert("onkeydown handler: \n"
      + "keyCode property: " + evt.keyCode + "\n"
      + "which property: " + evt.which + "\n"
     );
}


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

&lt;body
 onkeypress="showKeyPress(event);"
 onkeydown="keyDown(event);"
&gt;

&lt;p&gt;Please press any key.&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>

<h2 id="Specifications" name="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('DOM3 Events','#widl-KeyboardEvent-which','KeyboardEvent.which')}}</td>
   <td>{{Spec2('DOM3 Events')}}</td>
   <td>Initial definition; specified as deprecated</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>4</td>
   <td>2 [1]</td>
   <td>9</td>
   <td>10.10</td>
   <td>5.1</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown }}[1]</td>
   <td>10</td>
   <td>{{ CompatUnknown() }}</td>
   <td>5.1</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Gecko implements this property on the {{domxref("UIEvent")}} interface.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("KeyboardEvent")}}, the interface this property belongs too.</li>
</ul>
Revert to this revision