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 1065942 of KeyboardEvent

  • Revision slug: Web/API/KeyboardEvent
  • Revision title: KeyboardEvent
  • Revision id: 1065942
  • Created:
  • Creator: hashedhyphen
  • Is current revision? No
  • Comment Update the example to use KeyboardEvent.key

Revision Content

{{APIRef("DOM Events")}}

KeyboardEvent objects describe a user interaction with the keyboard. Each event describes a key; the event type ({{domevent('keydown')}}, {{domevent('keypress')}}, or {{domevent('keyup')}}) identifies what kind of activity was performed.

Note: The KeyboardEvent indicates just what's happening on a key. When you need to handle text input, use HTML5 input event instead. For example, if user inputs text from hand-writing system like tablet PC, key events may not be fired.

Constructor

{{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}}
Creates a KeyboardEvent object.

Methods

This interface also inherits methods of its parents, {{domxref("UIEvent")}} and {{domxref("Event")}}.

{{domxref("KeyboardEvent.getModifierState()")}}
Returns a {{jsxref("Boolean")}} indicating if the modifier key, like AltShiftCtrl, or Meta, was pressed when the event was created.
{{domxref("KeyboardEvent.initKeyEvent()")}}{{deprecated_inline}}
Initializes a KeyboardEvent object. This has only been implemented by Gecko (others used {{domxref("KeyboardEvent.initKeyboardEvent()")}}) and should not be used any more. The standard modern way is to use the {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}} constructor.
{{domxref("KeyboardEvent.initKeyboardEvent()")}}{{deprecated_inline}}
Initializes a KeyboardEvent object. This has never been implemented by Gecko (who used {{domxref("KeyboardEvent.initKeyEvent()")}}) and should not be used any more. The standard modern way is to use the {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}} constructor.

Properties

This interface also inherits properties of its parents, {{domxref("UIEvent")}} and {{domxref("Event")}}.

{{domxref("KeyboardEvent.altKey")}} {{Readonlyinline}}
Returns a {{jsxref("Boolean")}} that is true if the Alt ( Option or on OS X) key was active when the key event was generated.
{{domxref("KeyboardEvent.char")}} {{Non-standard_inline}}{{Deprecated_inline}}{{Readonlyinline}}
Returns a {{domxref("DOMString")}} representing the character value of the key. If the key corresponds to a printable character, this value is a non-empty Unicode string containing that character. If the key doesn't have a printable representation, this is an empty string.
Note: If the key is used as a macro that inserts multiple characters, this attribute's value is the entire string, not just the first character.
Warning: This has been dropped from DOM Level 3 Events. This is supported only on IE9+ and Microsoft Edge.
{{domxref("KeyboardEvent.charCode")}} {{Deprecated_inline}}{{Readonlyinline}}
Returns a {{jsxref("Number")}} representing the Unicode reference number of the key; this attribute is used only by the {{domevent('keypress')}} event. For keys whose char attribute contains multiple characters, this is the Unicode value of the first character in that attribute. In Firefox 26 this returns codes for printable characters.
Warning: This attribute is deprecated; you should use {{domxref("KeyboardEvent.key")}} instead, if available.
{{domxref("KeyboardEvent.code")}} {{Readonlyinline}}
Returns a {{domxref("DOMString")}} with the code value of the key represented by the event.
{{domxref("KeyboardEvent.ctrlKey")}} {{Readonlyinline}}
Returns a {{jsxref("Boolean")}} that is true if the Ctrl key was active when the key event was generated.
{{domxref("KeyboardEvent.isComposing")}} {{Readonlyinline}}
Returns a {{jsxref("Boolean")}} that is true if the event is fired between after {{domeventxref("compositionstart")}} and before {{domeventxref("compositionend")}}.
{{domxref("KeyboardEvent.key")}} {{Readonlyinline}}
Returns a {{domxref("DOMString")}} representing the key value of the key represented by the event.
{{domxref("KeyboardEvent.keyCode")}} {{deprecated_inline}}{{Readonlyinline}}
Returns a {{jsxref("Number")}} representing a system and implementation dependent numerical code identifying the unmodified value of the pressed key.
Warning: This attribute is deprecated; you should use {{domxref("KeyboardEvent.key")}} instead, if available.
{{domxref("KeyboardEvent.keyIdentifier")}} {{Non-standard_inline}}{{deprecated_inline}}{{Readonlyinline}}
This property is non-standard and has been deprecated in favor of {{domxref("KeyboardEvent.key")}}. It was part of an old version of DOM Level 3 Events.
{{domxref("KeyboardEvent.keyLocation")}} {{Non-standard_inline}}{{deprecated_inline}}{{Readonlyinline}}
This is a non-standard deprecated alias for {{domxref("KeyboardEvent.location")}}. It was part of an old version of DOM Level 3 Events.
{{domxref("KeyboardEvent.locale")}} {{Readonlyinline}}
Returns a {{domxref("DOMString")}} representing a locale string indicating the locale the keyboard is configured for. This may be the empty string if the browser or device doesn't know the keyboard's locale.
Note: This does not describe the locale of the data being entered. A user may be using one keyboard layout while typing text in a different language.
{{domxref("KeyboardEvent.location")}} {{Readonlyinline}}
Returns a {{jsxref("Number")}} representing the location of the key on the keyboard or other input device.
{{domxref("KeyboardEvent.metaKey")}} {{Readonlyinline}}
Returns a {{jsxref("Boolean")}} that is true if the Meta key (on Mac keyboards, the ⌘ Command key; on Windows keyboards, the Windows key ()) was active when the key event was generated.
{{domxref("KeyboardEvent.repeat")}} {{Readonlyinline}}
Returns a {{jsxref("Boolean")}} that is true if the key is being held down such that it is automatically repeating.
{{domxref("KeyboardEvent.shiftKey")}} {{Readonlyinline}}
Returns a {{jsxref("Boolean")}} that is true if the Shift key was active when the key event was generated.
{{domxref("KeyboardEvent.which")}} {{deprecated_inline}}{{Readonlyinline}}
Returns a {{jsxref("Number")}} representing a system and implementation dependent numeric code identifying the unmodified value of the pressed key; this is usually the same as keyCode.
Warning: This attribute is deprecated; you should use {{domxref("KeyboardEvent.key")}} instead, if available.

Notes

There are keydown, keypress, and keyup events. For most keys, Gecko dispatches a sequence of key events like this:

  1. When the key is first depressed, the keydown event is sent.
  2. If the key is not a modifier key, the keypress event is sent.
  3. When the user releases the key, the keyup event is sent.

Special cases

Certain keys toggle the state of an LED indicator, such as Caps Lock, Num Lock, and Scroll Lock. On Windows and Linux, these keys dispatch only the keydown and keyup events. Note that on Linux, Firefox 12 and earlier also dispatched the keypress event for these keys.

On Mac, however, Caps Lock dispatches only the keydown event due to a platform event model limitation. Num Lock had been supported on old MacBook (2007 model and older) but Mac hasn't supported Num Lock feature even on external keyboards in these days. On the old MacBook which has Num Lock key, Num Lock doesn't cause any key events. And Gecko supports Scroll Lock key if an external keyboard which has F14 is connected. However, it generates keypress event. This inconsistent behavior is a bug; see {{bug(602812)}}.

Auto-repeat handling

When a key is pressed and held down, it begins to auto-repeat. This results in a sequence of events similar to the following being dispatched:

  1. keydown
  2. keypress
  3. keydown
  4. keypress
  5. <<repeating until the user releases the key>>
  6. keyup

This is what the DOM Level 3 specification says should happen. There are some caveats, however, as described below.

Auto-repeat on some GTK environments such as Ubuntu 9.4

In some GTK-based environments, auto-repeat dispatches a native key-up event automatically during auto-repeat, and there's no way for Gecko to know the difference between a repeated series of keypresses and an auto-repeat. On those platforms, then, an auto-repeat key will generate the following sequence of events:

  1. keydown
  2. keypress
  3. keyup
  4. keydown
  5. keypress
  6. keyup
  7. <<repeating until the user releases the key>>
  8. keyup

In these environments, unfortunately, there's no way for web content to tell the difference between auto-repeating keys and keys that are just being pressed repeatedly.

Auto-repeat handling prior to Gecko 5.0

Before Gecko 5.0 {{geckoRelease('5.0')}}, keyboard handling was less consistent across platforms.

Windows
Auto-repeat behavior is the same as in Gecko 4.0 and later.
Mac
After the initial keydown event, only keypress events are sent until the keyup event occurs; the inter-spaced keydown events are not sent.
Linux
The event behavior depends on the specific platform. It will either behave like Windows or Mac depending on what the native event model does.

Note: manually firing an event does not generate the default action associated with that event. For example, manually firing a key event does not cause that letter to appear in a focused text input. In the case of UI events, this is important for security reasons, as it prevents scripts from simulating user actions that interact with the browser itself.

Example

<!DOCTYPE html>
<html>
<head>
<script>
'use strict';

document.addEventListener('keydown', (event) => {
  const keyName = event.key;

  if (keyName === 'Control') {
    // not alert when only Control key is pressed.
    return;
  }

  if (event.ctrlKey) {
    // Even though event.key is not 'Control' (i.e. 'a' is pressed),
    // event.ctrlKey may be true if Ctrl key is pressed at the time.
    alert(`Combination of ctrlKey + ${keyName}`);
  } else {
    alert(`Key pressed ${keyName}`);
  }
}, false);

document.addEventListener('keyup', (event) => {
  const keyName = event.key;

  // As the user release the Ctrl key, the key is no longer active.
  // So event.ctrlKey is false.
  if (keyName === 'Control') {
    alert('Control key was released');
  }
}, false);

</script>
</head>

<body>
</body>
</html>

Specifications

Specification Status Comment
{{SpecName('DOM3 Events', '#interface-KeyboardEvent', 'KeyboardEvent')}} {{Spec2('DOM3 Events')}} Initial definition

The KeyboardEvent interface specification went through numerous draft versions, first under DOM Events Level 2 where it was dropped as no consensus arose, then under DOM Events Level 3. This led to the implementation of non-standard initialization methods, the early DOM Events Level 2 version, {{domxref("KeyboardEvent.initKeyEvent()")}} by Gecko browsers and the early DOM Events Level 3 version, {{domxref("KeyboardEvent.initKeyboardEvent()")}} by others. Both have been superseded by the modern usage of a constructor: {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}}.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
constructor {{CompatVersionUnknown}} {{CompatGeckoDesktop("31.0")}} {{CompatNo}} {{CompatVersionUnknown}} {{CompatUnknown}}
.char {{CompatNo}} {{CompatNo}} 9 {{CompatNo}} {{CompatNo}}
.charCode {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
.isComposing {{CompatNo}} {{CompatGeckoDesktop("31.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
.keyCode {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
.locale {{CompatNo}} {{CompatNo}} {{CompatVersionUnknown}} {{CompatNo}} {{CompatNo}}
.location {{CompatVersionUnknown}} {{CompatGeckoDesktop("15.0")}} {{CompatVersionUnknown}} {{CompatNo}} {{CompatNo}}
.repeat {{CompatVersionUnknown}} {{CompatGeckoDesktop("28.0")}} {{CompatVersionUnknown}} {{CompatNo}} {{CompatNo}}
.which {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
.initKeyboardEvent() {{CompatVersionUnknown}}[1] {{CompatNo}}[2] {{CompatIE("9.0")}}[3] {{CompatUnknown}} {{CompatVersionUnknown}}[1]
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
constructor {{CompatUnknown}} {{CompatGeckoMobile("31.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
.char {{CompatUnknown}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
.charCode {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
.isComposing {{CompatNo}} {{CompatGeckoMobile("31.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
.keyCode {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
.locale {{CompatUnknown}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
.location {{CompatUnknown}} {{CompatGeckoMobile("15.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
.repeat {{CompatUnknown}} {{CompatGeckoMobile("28.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
.which {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
.initKeyboardEvent() {{CompatUnknown}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

[1] The arguments of initKeyboardEvent() of WebKit and Blink's are different from the definition in DOM Level 3 Events. The method is: initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString keyIdentifierArg, in number locationArg, in boolean ctrlKeyArg, in boolean altKeyArg, in boolean shiftKeyArg, in boolean metaKeyArg, in boolean altGraphKeyArg)

[2] Gecko won't support initKeyboardEvent() because supporting it completely breaks feature detection of web applications. See {{Bug(999645)}}.

[3] The argument of initKeyboardEvent() of IE is different from the definition in DOM Level 3 Events. The method is: initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString keyArg, in number locationArg, in DOMString modifierListArg, in boolean repeatArt, in DOMString locationArg). See document of initKeyboardEvent() in MSDN.

See also

  • {{domxref("KeyboardEvent.code")}}.
  • {{domxref("KeyboardEvent.key")}}.
  • {{domxref("KeyboardEvent.getModifierState")}}

Revision Source

<div>{{APIRef("DOM Events")}}</div>

<p><strong><code>KeyboardEvent</code></strong> objects describe a user interaction with the keyboard. Each event describes a key; the event type ({{domevent('keydown')}}, {{domevent('keypress')}}, or {{domevent('keyup')}}) identifies what kind of activity was performed.</p>

<div class="note"><strong>Note:</strong> The <code>KeyboardEvent</code> indicates just what's happening on a key. When you need to handle text input, use HTML5 <code><a href="/en-US/docs/DOM/DOM_event_reference/input" rel="custom">input</a></code> event instead. For example, if user inputs text from hand-writing system like tablet PC, key events may not be fired.</div>

<h2 id="Constructor">Constructor</h2>

<dl>
 <dt>{{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}}</dt>
 <dd>Creates a <code>KeyboardEvent</code> object.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<p><em>This interface also inherits methods of its parents, {{domxref("UIEvent")}} and {{domxref("Event")}}.</em></p>

<dl>
 <dt>{{domxref("KeyboardEvent.getModifierState()")}}</dt>
 <dd>Returns a {{jsxref("Boolean")}} indicating if the modifier key, like&nbsp;<kbd>Alt</kbd>,&nbsp;<kbd>Shift</kbd>,&nbsp;<kbd>Ctrl</kbd>, or <kbd>Meta</kbd>, was pressed when the event was created.</dd>
 <dt>{{domxref("KeyboardEvent.initKeyEvent()")}}{{deprecated_inline}}</dt>
 <dd>Initializes a <code>KeyboardEvent</code> object. This has only been implemented by Gecko (others used {{domxref("KeyboardEvent.initKeyboardEvent()")}}) and should not be used any more. The standard modern way is to use the {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}} constructor.</dd>
 <dt>{{domxref("KeyboardEvent.initKeyboardEvent()")}}{{deprecated_inline}}</dt>
 <dd>Initializes a <code>KeyboardEvent</code> object. This has never been implemented by Gecko (who used {{domxref("KeyboardEvent.initKeyEvent()")}}) and should not be used any more. The standard modern way is to use the {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}} constructor.</dd>
</dl>

<h2 id="Properties">Properties</h2>

<p><em>This interface also inherits properties of its parents, {{domxref("UIEvent")}} and {{domxref("Event")}}.</em></p>

<dl>
 <dt>{{domxref("KeyboardEvent.altKey")}} {{Readonlyinline}}</dt>
 <dd>Returns a {{jsxref("Boolean")}} that is <code>true</code> if the <kbd>Alt</kbd> ( <kbd>Option</kbd> or <kbd>⌥</kbd> on OS X) key was active when the key event was generated.</dd>
 <dt>{{domxref("KeyboardEvent.char")}} {{Non-standard_inline}}{{Deprecated_inline}}{{Readonlyinline}}</dt>
 <dd>Returns a {{domxref("DOMString")}} representing the character value of the key. If the key corresponds to a printable character, this value is a non-empty Unicode string containing that character. If the key doesn't have a printable representation, this is an empty string.
 <div class="note"><strong>Note:</strong> If the key is used as a macro that inserts multiple characters, this attribute's value is the entire string, not just the first character.</div>

 <div class="warning"><strong>Warning:</strong> This has been dropped from DOM Level 3 Events. This is supported only on IE9+ and Microsoft Edge.</div>
 </dd>
 <dt>{{domxref("KeyboardEvent.charCode")}} {{Deprecated_inline}}{{Readonlyinline}}</dt>
 <dd>Returns a&nbsp;{{jsxref("Number")}} representing the Unicode reference number of the key; this attribute is used only by the {{domevent('keypress')}} event. For keys whose <code>char</code> attribute contains multiple characters, this is the Unicode value of the first character in that attribute. In Firefox 26 this returns codes for printable characters.
 <div class="warning"><strong>Warning:</strong> This attribute is deprecated; you should use {{domxref("KeyboardEvent.key")}} instead, if available.</div>
 </dd>
 <dt>{{domxref("KeyboardEvent.code")}} {{Readonlyinline}}</dt>
 <dd>Returns a {{domxref("DOMString")}} with the code value of the key represented by the event.</dd>
 <dt>{{domxref("KeyboardEvent.ctrlKey")}} {{Readonlyinline}}</dt>
 <dd>Returns a {{jsxref("Boolean")}} that is <code>true</code> if the <kbd>Ctrl</kbd> key was active when the key event was generated.</dd>
 <dt>{{domxref("KeyboardEvent.isComposing")}} {{Readonlyinline}}</dt>
 <dd>Returns a {{jsxref("Boolean")}} that is <code>true</code> if the event is fired between after {{domeventxref("compositionstart")}} and before {{domeventxref("compositionend")}}.</dd>
 <dt>{{domxref("KeyboardEvent.key")}} {{Readonlyinline}}</dt>
 <dd>Returns a {{domxref("DOMString")}} representing the key value of the key represented by the event.</dd>
 <dt>{{domxref("KeyboardEvent.keyCode")}}&nbsp;{{deprecated_inline}}{{Readonlyinline}}</dt>
 <dd>Returns a&nbsp;{{jsxref("Number")}}&nbsp;representing a system and implementation dependent numerical code identifying the unmodified value of the pressed key.
 <div class="warning"><strong>Warning:</strong> This attribute is deprecated; you should use {{domxref("KeyboardEvent.key")}} instead, if available.</div>
 </dd>
 <dt>{{domxref("KeyboardEvent.keyIdentifier")}} {{Non-standard_inline}}{{deprecated_inline}}{{Readonlyinline}}</dt>
 <dd>This property is non-standard and has been deprecated in favor of {{domxref("KeyboardEvent.key")}}. It was part of an old version of DOM Level 3 Events.</dd>
 <dt>{{domxref("KeyboardEvent.keyLocation")}} {{Non-standard_inline}}{{deprecated_inline}}{{Readonlyinline}}</dt>
 <dd>This is a non-standard deprecated alias for {{domxref("KeyboardEvent.location")}}. It was part of an old version of DOM Level 3 Events.</dd>
 <dt>{{domxref("KeyboardEvent.locale")}} {{Readonlyinline}}</dt>
 <dd>Returns a {{domxref("DOMString")}} representing a locale string indicating the locale the keyboard is configured for. This may be the empty string if the browser or device doesn't know the keyboard's locale.
 <div class="note"><strong>Note:</strong> This does not describe the locale of the data being entered. A user may be using one keyboard layout while typing text in a different language.</div>
 </dd>
 <dt>{{domxref("KeyboardEvent.location")}}<a> {{Readonlyinline}}</a></dt>
 <dd>Returns a&nbsp;{{jsxref("Number")}} representing the location of the key on the keyboard or other input device.</dd>
 <dt>{{domxref("KeyboardEvent.metaKey")}} {{Readonlyinline}}</dt>
 <dd>Returns a {{jsxref("Boolean")}} that is <code>true</code> if the <kbd>Meta</kbd> key (on Mac keyboards, the <kbd>⌘ Command</kbd> key; on Windows keyboards, the Windows key (<kbd>⊞</kbd>)) was active when the key event was generated.</dd>
 <dt>{{domxref("KeyboardEvent.repeat")}} {{Readonlyinline}}</dt>
 <dd>Returns a {{jsxref("Boolean")}} that is <code>true</code> if the key is being held down such that it is automatically repeating.</dd>
 <dt>{{domxref("KeyboardEvent.shiftKey")}} {{Readonlyinline}}</dt>
 <dd>Returns a {{jsxref("Boolean")}} that is <code>true</code> if the <kbd>Shift</kbd> key was active when the key event was generated.</dd>
 <dt>{{domxref("KeyboardEvent.which")}} {{deprecated_inline}}{{Readonlyinline}}</dt>
 <dd>Returns a&nbsp;{{jsxref("Number")}} representing a system and implementation dependent numeric code identifying the unmodified value of the pressed key; this is usually the same as <code>keyCode</code>.
 <div class="warning"><strong>Warning:</strong> This attribute is deprecated; you should use {{domxref("KeyboardEvent.key")}} instead, if available.</div>
 </dd>
</dl>

<h2 id="Notes">Notes</h2>

<p>There are <code>keydown</code>, <code>keypress</code>, and <code>keyup</code> events. For most keys, Gecko dispatches a sequence of key events like this:</p>

<ol>
 <li>When the key is first depressed, the <code>keydown</code> event is sent.</li>
 <li>If the key is not a modifier key, the <code>keypress</code> event is sent.</li>
 <li>When the user releases the key, the <code>keyup</code> event is sent.</li>
</ol>

<h3 id="Special_cases">Special cases</h3>

<p>Certain keys toggle the state of an LED indicator, such as Caps Lock, Num Lock, and Scroll Lock. On Windows and Linux, these keys dispatch only the <code>keydown</code> and <code>keyup</code> events. Note that on Linux, Firefox 12 and earlier also dispatched the <code>keypress</code> event for these keys.</p>

<p>On Mac, however, Caps Lock dispatches only the <code>keydown</code> event due to a platform event model limitation. Num Lock had been supported on old MacBook (2007 model and older) but Mac hasn't supported Num Lock feature even on external keyboards in these days. On the old MacBook which has Num Lock key, Num Lock doesn't cause any key events. And Gecko supports Scroll Lock key if an external keyboard which has F14 is connected. However, it generates <code>keypress</code> event. This inconsistent behavior is a bug; see {{bug(602812)}}.</p>

<h3 id="Auto-repeat_handling">Auto-repeat handling</h3>

<p>When a key is pressed and held down, it begins to auto-repeat. This results in a sequence of events similar to the following being dispatched:</p>

<ol>
 <li><code>keydown</code></li>
 <li><code>keypress</code></li>
 <li><code>keydown</code></li>
 <li><code>keypress</code></li>
 <li>&lt;&lt;repeating until the user releases the key&gt;&gt;</li>
 <li><code>keyup</code></li>
</ol>

<p>This is what the DOM Level 3 specification says should happen. There are some caveats, however, as described below.</p>

<h4 id="Auto-repeat_on_some_GTK_environments_such_as_Ubuntu_9.4">Auto-repeat on some GTK environments such as Ubuntu 9.4</h4>

<p>In some GTK-based environments, auto-repeat dispatches a native key-up event automatically during auto-repeat, and there's no way for Gecko to know the difference between a repeated series of keypresses and an auto-repeat. On those platforms, then, an auto-repeat key will generate the following sequence of events:</p>

<ol>
 <li><code>keydown</code></li>
 <li><code>keypress</code></li>
 <li><code>keyup</code></li>
 <li><code>keydown</code></li>
 <li><code>keypress</code></li>
 <li><code>keyup</code></li>
 <li>&lt;&lt;repeating until the user releases the key&gt;&gt;</li>
 <li><code>keyup</code></li>
</ol>

<p>In these environments, unfortunately, there's no way for web content to tell the difference between auto-repeating keys and keys that are just being pressed repeatedly.</p>

<h4 id="Auto-repeat_handling_prior_to_Gecko_5.0">Auto-repeat handling prior to Gecko 5.0</h4>

<p>Before Gecko 5.0 {{geckoRelease('5.0')}}, keyboard handling was less consistent across platforms.</p>

<dl>
 <dt>Windows</dt>
 <dd>Auto-repeat behavior is the same as in Gecko 4.0 and later.</dd>
 <dt>Mac</dt>
 <dd>After the initial keydown event, only keypress events are sent until the keyup event occurs; the inter-spaced keydown events are not sent.</dd>
 <dt>Linux</dt>
 <dd>The event behavior depends on the specific platform. It will either behave like Windows or Mac depending on what the native event model does.</dd>
</dl>

<p class="note"><strong>Note:</strong> manually firing an event does <em>not</em> generate the default action associated with that event. For example, manually firing a key event does not cause that letter to appear in a focused text input. In the case of UI events, this is important for security reasons, as it prevents scripts from simulating user actions that interact with the browser itself.</p>

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

<pre class="brush: js">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;script&gt;
'use strict';

document.addEventListener('keydown', (event) =&gt; {
  const keyName = event.key;

  if (keyName === 'Control') {
    // not alert when only Control key is pressed.
    return;
  }

&nbsp; if (event.ctrlKey) {
&nbsp;&nbsp;&nbsp; // Even though event.key is not 'Control' (i.e. 'a' is pressed),
&nbsp;&nbsp;&nbsp; // event.ctrlKey may be true if Ctrl key is pressed at the time.
&nbsp;&nbsp;&nbsp; alert(`Combination of ctrlKey + ${keyName}`);
&nbsp; } else {
&nbsp;&nbsp;&nbsp; alert(`Key pressed ${keyName}`);
&nbsp; }
}, false);

document.addEventListener('keyup', (event) =&gt; {
&nbsp; const keyName = event.key;

&nbsp; // As the user release the Ctrl key, the key is no longer active.
&nbsp; // So event.ctrlKey is false.
&nbsp; if (keyName === 'Control') {
&nbsp;&nbsp;&nbsp; alert('Control key was released');
&nbsp; }
}, false);

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

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

<h2 id="Specifications">Specifications</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', '#interface-KeyboardEvent', 'KeyboardEvent')}}</td>
   <td>{{Spec2('DOM3 Events')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<p>The <code>KeyboardEvent</code> interface specification went through numerous draft versions, first under DOM Events Level 2 where it was dropped as no consensus arose, then under DOM Events Level 3. This led to the implementation of non-standard initialization methods, the early DOM Events Level 2 version, {{domxref("KeyboardEvent.initKeyEvent()")}} by Gecko browsers and the early DOM Events Level 3 version, {{domxref("KeyboardEvent.initKeyboardEvent()")}} by others. Both have been superseded by the modern usage of a constructor: {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}}.</p>

<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>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>constructor</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("31.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>.char</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>9</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>.charCode</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>.isComposing</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop("31.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>.keyCode</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>.locale</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>.location</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("15.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>.repeat</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("28.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>.which</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>.initKeyboardEvent()</code></td>
   <td>{{CompatVersionUnknown}}<sup>[1]</sup></td>
   <td>{{CompatNo}}<sup>[2]</sup></td>
   <td>{{CompatIE("9.0")}}<sup>[3]</sup></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}<sup>[1]</sup></td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE&nbsp;Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>constructor</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("31.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>.char</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>.charCode</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>.isComposing</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile("31.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>.keyCode</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>.locale</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>.location</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("15.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>.repeat</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("28.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>.which</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>.initKeyboardEvent()</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] The arguments of <code>initKeyboardEvent()</code> of WebKit and Blink's are different from the definition in DOM Level 3 Events. The method is: <code>initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString keyIdentifierArg, in number locationArg, in boolean ctrlKeyArg, in boolean altKeyArg, in boolean shiftKeyArg, in boolean metaKeyArg, in boolean altGraphKeyArg)</code></p>

<p>[2] Gecko won't support <code>initKeyboardEvent()</code> because supporting it completely breaks feature detection of web applications. See {{Bug(999645)}}.</p>

<p>[3] The argument of <code>initKeyboardEvent()</code> of IE is different from the definition in DOM Level 3 Events. The method is: <code>initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString keyArg, in number locationArg, in DOMString modifierListArg, in boolean repeatArt, in DOMString locationArg)</code>. See <a href="https://msdn.microsoft.com/en-us/library/ie/ff975297%28v=vs.85%29.aspx">document of <code>initKeyboardEvent()</code> in MSDN</a>.</p>

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

<ul>
 <li>{{domxref("KeyboardEvent.code")}}.</li>
 <li>{{domxref("KeyboardEvent.key")}}.</li>
 <li>{{domxref("KeyboardEvent.getModifierState")}}</li>
</ul>
Revert to this revision