--- title: KeyboardEvent slug: Web/API/KeyboardEvent tags: - API translation_of: Web/API/KeyboardEvent ---
{{APIRef("DOM Events")}}
Los objetos KeyboardEvent
describen una interacción del usuario con el teclado. Cada evento describe una tecla; el tipo de evento(keydown
, keypress
, o keyup
) identifica el tipo de acción realizada.
KeyboardEvent
solo indica qué está pasando en una tecla. Cuando necesite manejar la entrada de texto, use el evento input
de HTML5 en su lugar. Por ejemplo, si el usuario introduce texto desde un sistema de tipo manuscrito como una tableta, los eventos para teclas no podrán ser lanzados.KeyboardEvent
.Esta interfaz también hereda métodos de sus padres, {{domxref("UIEvent")}} and {{domxref("Event")}}.
KeyboardEvent
. Este método solo ha sido implementado por Gecko (otros usados {{domxref("KeyboardEvent.initKeyboardEvent()")}}) y nunca más será usado. El modo estándar moderno es usar el constructor {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}}.KeyboardEvent
. Este método nunca fue implementado por Gecko (quien usa {{domxref("KeyboardEvent.initKeyEvent()")}}) y no debe ser utilizado más. El modo estándar moderno es usar el constructor {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}}.Esta interfaz también hereda propiedades de sus padres {{domxref("UIEvent")}} y {{domxref("Event")}}.
true
si la tecla Alt ( Option or ⌥ on OS X) fue activada cuando el evento fue generado.unsigned
long
representing the Unicode reference number of the key; this attribute is used only by the 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.
key
instead, if available.true
if the Ctrl key was active when the key event was generated.true
if the event is fired between after compositionstart
and before compositionend
.unsigned long
representing a system and implementation dependent numerical code identifying the unmodified value of the pressed key.
key
instead, if available.unsigned long
representing the location of the key on the keyboard or other input device.true
if the Meta (or Command on OS X) key was active when the key event was generated.true
if the key is being held down such that it is automatically repeating.true
if the Shift key was active when the key event was generated.unsigned long
representing a system and implementation dependent numeric code identifying the unmodified value of the pressed key; this is usually the same as keyCode
.
key
instead, if available.There are keydown
, keypress
, and keyup
events. For most keys, Gecko dispatches a sequence of key events like this:
keydown
event is sent.keypress
event is sent.keyup
event is sent.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)}}.
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:
keydown
keypress
keydown
keypress
keyup
This is what the DOM Level 3 specification says should happen. There are some caveats, however, as described below.
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:
keydown
keypress
keyup
keydown
keypress
keyup
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.
Before Gecko 4.0 {{geckoRelease('4.0')}}, keyboard handling was less consistent across platforms.
<!DOCTYPE html> <html> <head> <script> var metaChar = false; var exampleKey = 16; function keyEvent(event) { var key = event.keyCode || event.which; var keychar = String.fromCharCode(key); if (key == exampleKey) { metaChar = true; } if (key != exampleKey) { if (metaChar) { alert("Combination of metaKey + " + keychar); metaChar = false; } else { alert("Key pressed " + key); } } } function metaKeyUp (event) { var key = event.keyCode || event.which; if (key == exampleKey) { metaChar = false; } } </script> </head> <body onkeydown="keyEvent(event)" onkeyup="metaKeyUp(event)"> </body> </html>
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()")}}.
{{ 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() }} | {{ CompatVersionUnknown() }} | {{ CompatNo() }} | {{ CompatNo() }} |
.charCode |
{{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} |
.code |
See Browser compatibility of {{domxref("KeyboardEvent.code")}}. | ||||
.isComposing |
{{ CompatNo() }} | {{ CompatGeckoDesktop("31.0") }} | {{ CompatNo() }} | {{ CompatNo() }} | {{ CompatNo() }} |
.key |
See Browser compatibility of {{domxref("KeyboardEvent.key")}}. | ||||
.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() }} |
.getModifierState() |
See Browser compatibility of {{domxref("KeyboardEvent.getModifierState")}} | ||||
.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() }} | |
.code |
See Browser compatibility of {{domxref("KeyboardEvent.code")}}. | |||||
.isComposing |
{{ CompatNo() }} | {{ CompatGeckoMobile("31.0") }} | {{ CompatNo() }} | {{ CompatNo() }} | {{ CompatNo() }} | |
.key |
See Browser compatibility table of {{domxref("KeyboardEvent.key")}}. | |||||
.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() }} | |
.getModifierState() |
See Browser compatibility of {{domxref("KeyboardEvent.getModifierState")}} | |||||
.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 keyIndentifierArg, in unsigned long 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 unsigned long locationArg, in DOMString modifierListArg, in boolean repeatArt, in DOMString locationArg)
. See document of initKeyboardEvent()
in MSDN.