KeyboardEvent
Objetos do tipo KeyboardEvent
descrevem a interação do usuário com o teclado. Cada evento descreve uma tecla; o tipo de evento (keydown
, keypress
, ou keyup
) identifica qual tipo de ação foi executada.
Nota: O KeyboardEvent
indica o que está havendo com a tecla. Quando você precisar lidar com entrada de texto, use o evento input
do HTML5. Por exemplo, se o usuário envia texto por meio de um sistema manuscrito como um tablet PC, eventos das teclas podem não ser disparados.
Construtor
KeyboardEvent()
(en-US)-
Cria um objeto do tipo
KeyboardEvent
.
Métodos
Essa interface também herda métodos dos seus pais, UIEvent
(en-US) e Event
.
KeyboardEvent.getModifierState()
(en-US)-
Retorna um
Boolean
indicando se uma tecla, comoAlt
,
Shift
,
Ctrl
, ou
Meta
, foi pressionada quando o evento foi criado.
KeyboardEvent.initKeyEvent()
(en-US) Deprecated-
Inicializa um objeto do tipo
KeyboardEvent
. isso foi apenas implementado por Gecko (outros usaramKeyboardEvent.initKeyboardEvent()
(en-US)) e não deve mais ser usado. A forma moderna padrão é usar o construtorKeyboardEvent()
(en-US). KeyboardEvent.initKeyboardEvent()
(en-US) Deprecated-
Inicializa um objeto do tipo
KeyboardEvent
. Isso nunca foi implementado por Gecko (que usouKeyboardEvent.initKeyEvent()
(en-US)) e não deve mais ser usado. A forma moderna padrão é usar o construtorKeyboardEvent()
(en-US).
Propriedades
Essa interface também herda métodos dos seus pais, UIEvent
(en-US) e Event
.
KeyboardEvent.altKey
(en-US) Somente leitura-
Retorna um
Boolean
que étrue
se a teclaAlt
(
Option
ou
⌥
no OS X) estava ativa quando o evento foi acionado.
KeyboardEvent.char
Non-standard Deprecated Somente leitura-
Retorna um
DOMString
representando o valor da tecla em caractere. Se a tecla corresponde a um caractere exibível, esse valor é uma string Unicode não-vazia contendo aquele caractere. Se a tecla não possui uma representação exibível, ela é uma string vazia.Nota: Se a tecla é usada como um macro que insere caracteres múltiplos, o valor desse atributo é a string inteira, não apenas o primeiro caractere.
Aviso: Isso foi removido dos Eventos DOM de nível 3. Isso é apenas suportado no IE.
KeyboardEvent.charCode
(en-US) Deprecated Somente leitura-
Returns a
Number
representing the Unicode reference number of the key; this attribute is used only by thekeypress
event. For keys whosechar
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.Aviso: Este atributo esta depreciado; você deve usar
key
no lugar, se disponivel. KeyboardEvent.code
(en-US) Somente leitura-
Returns a
DOMString
with the code value of the key represented by the event. KeyboardEvent.ctrlKey
(en-US) Somente leitura-
Returns a
Boolean
that istrue
if theCtrl
key was active when the key event was generated.
KeyboardEvent.isComposing
(en-US) Somente leitura-
Returns a
Boolean
that istrue
if the event is fired between aftercompositionstart
and beforecompositionend
. KeyboardEvent.key
(en-US) Somente leitura-
Returns a
DOMString
representing the key value of the key represented by the event. KeyboardEvent.keyCode
(en-US) Deprecated Somente leitura-
Returns a
Number
representing a system and implementation dependent numerical code identifying the unmodified value of the pressed key.Aviso: Este atributo esta depreciado; você deve usar
key
no lugar, se disponivel. KeyboardEvent.locale
Somente leitura-
Returns a
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.Nota: 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.
KeyboardEvent.location
(en-US) Somente leitura-
Returns a
Number
representing the location of the key on the keyboard or other input device. KeyboardEvent.metaKey
(en-US) Somente leitura-
Returns a
Boolean
that istrue
if theMeta
(or
Command
on OS X) key was active when the key event was generated.
KeyboardEvent.repeat
(en-US) Somente leitura-
Returns a
Boolean
that istrue
if the key is being held down such that it is automatically repeating. KeyboardEvent.shiftKey
(en-US) Somente leitura-
Returns a
Boolean
that istrue
if theShift
key was active when the key event was generated.
KeyboardEvent.which
(en-US) Deprecated Somente leitura-
Returns a
Number
representing a system and implementation dependent numeric code identifying the unmodified value of the pressed key; this is usually the same askeyCode
.Aviso: Este atributo esta depreciado; você deve usar
key
no lugar, se disponivel.
Notes
There are keydown
, keypress
, and keyup
events. For most keys, Gecko dispatches a sequence of key events like this:
- When the key is first depressed, the
keydown
event is sent. - If the key is not a modifier key, the
keypress
event is sent. - 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:
keydown
keypress
- <<repeating until the user releases the key>>
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:
keydown
keypress
- <<repeating until the user releases the key>>
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 4.0
Before Gecko 4.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.
Example
<!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>
Specifications
Specification | Status | Comment |
---|---|---|
Document Object Model (DOM) Level 3 Events Specification The definition of 'KeyboardEvent' in that specification. |
Obsoleto | 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, KeyboardEvent.initKeyEvent()
(en-US) by Gecko browsers and the early DOM Events Level 3 version, KeyboardEvent.initKeyboardEvent()
(en-US) by others. Both have been superseded by the modern usage of a constructor: KeyboardEvent()
(en-US).
Browser compatibility
BCD tables only load in the browser