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 1109735 of KeyboardEvent.key

  • Revision slug: Web/API/KeyboardEvent/key
  • Revision title: KeyboardEvent.key
  • Revision id: 1109735
  • Created:
  • Creator: stephaniehobson
  • Is current revision? No
  • Comment

Revision Content

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

The KeyboardEvent.key read-only property returns the value of a key or keys pressed by the user. Its value is determined as follows:

See a full list of key values.

  • If the pressed key has a printed representation, the returned value is a non-empty Unicode character string containing the printable representation of the key.
  • If the pressed key is a control or special character, the returned value is one of the pre-defined key values.
  • If the KeyboardEvent represents the press of a dead key, the key value must be "Dead".
  • If more than one key is pressed and the combination includes a modifier that makes the resulting keystroke non printing, the returned value is the printable character. For example, if the combination were Control + a, the letter "a" is returned.
  • Some specialty keyboard keys (such as the extended keys for controlling media on multimedia keyboards) don't generate key codes on Windows; instead, they trigger WM_APPCOMMAND events. These events get mapped to DOM keyboard events, and are listed among the "Virtual key codes" for Windows, even though they aren't actually key codes.
  • If the key cannot be identified, the returned value is "Unidentified".

This page is undergoing heavy updating to improve its layout, modernize its content, and bring everything up to date. Newer content is nearer the top; to find the older tables, see {{anch("Previous versions of tables")}}. Once the updates are finished, that content will be removed. No information should be lost once it's all done.

Example

This example uses {{domxref("EventTarget.addEventListener()")}} to listen for {{event("keydown")}} events. When they occur,  the key's value is checked to see if it's one of the keys the code is interested in, and if it is, it gets processed in some way (possibly by steering a spacecraft, perhaps by changing the selected cell in a spreadsheet).

window.addEventListener("keydown", function (event) {
  if (event.defaultPrevented) {
    return; // Should do nothing if the key event was already consumed.
  }

  switch (event.key) {
    case "ArrowDown":
      // Do something for "down arrow" key press.
      break;
    case "ArrowUp":
      // Do something for "up arrow" key press.
      break;
    case "ArrowLeft":
      // Do something for "left arrow" key press.
      break;
    case "ArrowRight":
      // Do something for "right arrow" key press.
      break;
    case "Enter":
      // Do something for "enter" or "return" key press.
      break;
    case "Escape":
      // Do something for "esc" key press.
      break;
    default:
      return; // Quit when this doesn't handle the key event.
  }

  // Consume the event to avoid it being handled twice
  event.preventDefault();
}, true);

Specification

Specification Status Comment
{{SpecName('DOM3 Events', '#widl-KeyboardEvent-key', 'KeyboardEvent.key')}} {{Spec2('DOM3 Events')}} Initial definition, included key values.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support

{{CompatChrome(51.0)}}

{{CompatGeckoDesktop("23.0")}} 9.0[1] 38.0 {{CompatNo}} [2]
non-printable keys {{CompatChrome(51.0)}} {{CompatGeckoDesktop("23.0")}} 9.0[1] 38.0 {{CompatNo}}
printable keys {{CompatChrome(51.0)}} {{CompatGeckoDesktop("29.0")}} 9.0[1] 38.0 {{CompatNo}}
dead key {{CompatChrome(51.0)}} {{CompatNo}} {{CompatNo}} 38.0 {{CompatNo}}
s
Feature Android Android Webview Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatChrome(51.0)}} {{CompatGeckoMobile("23.0")}} {{CompatUnknown}} 38.0 {{CompatNo}} [2] {{CompatChrome(51.0)}}
non-printable keys of virtual keyboard {{CompatNo}} {{CompatChrome(51.0)}} {{CompatGeckoMobile("23.0")}} {{CompatUnknown}} 38.0 {{CompatNo}} {{CompatChrome(51.0)}}
printable keys of virtual keyboard {{CompatNo}} {{CompatChrome(51.0)}} {{CompatUnknown}} {{CompatUnknown}} 38.0 {{CompatNo}} {{CompatChrome(51.0)}}
non-printable keys of physical keyboard {{CompatNo}} {{CompatChrome(51.0)}} {{CompatGeckoMobile("23.0")}} {{CompatUnknown}} 38.0 {{CompatNo}} {{CompatChrome(51.0)}}
printable keys of physical keyboard {{CompatNo}} {{CompatChrome(51.0)}} {{CompatGeckoMobile("29.0")}} {{CompatUnknown}} 38.0 {{CompatNo}} {{CompatChrome(51.0)}}

[1]: Internet Explorer's implementation does not completely match the current spec because it is based on an older version of the spec.

[2]: WebKit bug #69029

Revision Source

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

<p><span class="seoSummary">The <code><strong>KeyboardEvent.key</strong></code> read-only property returns the value of a key or keys pressed by the user.</span> Its value is determined as follows:</p>

<div class="pull-aside">
<p class="moreinfo">See a full list of <a href="/docs/Web/API/KeyboardEvent/key/Key_Values">key values</a>.</p>
</div>

<ul>
 <li>If the pressed key has a printed representation, the returned value is a non-empty Unicode character string containing the printable representation of the key.</li>
 <li>If the pressed key is a control or special character, the returned value is one of the <a href="/docs/Web/API/KeyboardEvent/key/Key_Values">pre-defined key values</a>.</li>
 <li>If the <code>KeyboardEvent</code> represents the press of a dead key, the key value must be "<code>Dead</code>".</li>
 <li>If more than one key is pressed and the combination includes a modifier that makes the resulting keystroke non printing, the returned value is the printable character. For example, if the combination were Control + a, the letter "a" is returned.</li>
 <li>Some specialty keyboard keys (such as the extended keys for controlling media on multimedia keyboards) don't generate key codes on Windows; instead, they trigger <code>WM_APPCOMMAND</code> events. These events get mapped to DOM keyboard events, and are listed among the "Virtual key codes" for Windows, even though they aren't actually key codes.</li>
 <li>If the key cannot be identified, the returned value is <code>"Unidentified"</code>.</li>
</ul>

<div class="note">
<p>This page is undergoing heavy updating to improve its layout, modernize its content, and bring everything up to date. Newer content is nearer the top; to find the older tables, see {{anch("Previous versions of tables")}}. Once the updates are finished, that content will be removed. No information should be lost once it's all done.</p>
</div>

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

<p>This example uses {{domxref("EventTarget.addEventListener()")}} to listen for {{event("keydown")}} events. When they occur,&nbsp; the key's value is checked to see if it's one of the keys the code is interested in, and if it is, it gets processed in some way (possibly by steering a spacecraft, perhaps by changing the selected cell in a spreadsheet).</p>

<pre class="brush: js">
window.addEventListener("keydown", function (event) {
  if (event.defaultPrevented) {
    return; // Should do nothing if the key event was already consumed.
  }

  switch (event.key) {
    case "ArrowDown":
      // Do something for "down arrow" key press.
      break;
    case "ArrowUp":
      // Do something for "up arrow" key press.
      break;
    case "ArrowLeft":
      // Do something for "left arrow" key press.
      break;
    case "ArrowRight":
      // Do something for "right arrow" key press.
      break;
    case "Enter":
      // Do something for "enter" or "return" key press.
      break;
    case "<code>Escape</code>":
      // Do something for "esc" key press.
      break;
    default:
      return; // Quit when this doesn't handle the key event.
  }

  // Consume the event to avoid it being handled twice
  event.preventDefault();
}, true);
</pre>

<h2 id="Specification">Specification</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('DOM3 Events', '#widl-KeyboardEvent-key', 'KeyboardEvent.key')}}</td>
   <td>{{Spec2('DOM3 Events')}}</td>
   <td>Initial definition, included key values.</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 (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>
    <p>{{CompatChrome(51.0)}}</p>
   </td>
   <td>{{CompatGeckoDesktop("23.0")}}</td>
   <td>9.0[1]</td>
   <td>38.0</td>
   <td>{{CompatNo}} [2]</td>
  </tr>
  <tr>
   <td>non-printable keys</td>
   <td>{{CompatChrome(51.0)}}</td>
   <td>{{CompatGeckoDesktop("23.0")}}</td>
   <td>9.0[1]</td>
   <td>38.0</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>printable keys</td>
   <td>{{CompatChrome(51.0)}}</td>
   <td>{{CompatGeckoDesktop("29.0")}}</td>
   <td>9.0[1]</td>
   <td>38.0</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>dead key</td>
   <td>{{CompatChrome(51.0)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>38.0</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">s
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE&nbsp;Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(51.0)}}</td>
   <td>{{CompatGeckoMobile("23.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>38.0</td>
   <td>{{CompatNo}} [2]</td>
   <td>{{CompatChrome(51.0)}}</td>
  </tr>
  <tr>
   <td>non-printable keys of virtual keyboard</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(51.0)}}</td>
   <td>{{CompatGeckoMobile("23.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>38.0</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(51.0)}}</td>
  </tr>
  <tr>
   <td>printable keys of virtual keyboard</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(51.0)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>38.0</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(51.0)}}</td>
  </tr>
  <tr>
   <td>non-printable keys of physical keyboard</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(51.0)}}</td>
   <td>{{CompatGeckoMobile("23.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>38.0</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(51.0)}}</td>
  </tr>
  <tr>
   <td>printable keys of physical keyboard</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(51.0)}}</td>
   <td>{{CompatGeckoMobile("29.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>38.0</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(51.0)}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1]: Internet Explorer's implementation does not completely match the current spec because it is based on an older version of the spec.</p>

<p>[2]: <a href="https://bugs.webkit.org/show_bug.cgi?id=69029">WebKit bug #69029</a></p>
Revert to this revision