El evento keydown es lanzado cuando una tecla es presionada (hacia abajo). A diferencia del evento keypress, el evento keydown es lanzado para las teclas que producen un carácter y también para las que no lo producen.

Información general

Especificación
DOM L3
Interfaz
KeyboardEvent
Burbuja
Si
Cancelable
Si
Objetivo
Document, Element
Acción
Varía: evento keypress; lanza el sistema de composición de texto; eventos blur y focus; DOMActivate evento; otro evento

Propiedades

Propiedad Tipo Descripción
target Read only EventTarget Elemento con el foco que procesa el evento, elemento raíz si no hay un elemento adecuado de entrada de texto con el foco.
type Read only DOMString El tipo de evento.
bubbles Read only Boolean Si el elemento normalmente es burbuja o no
cancelable Read only Boolean Si el elemento es cancelable o no
view Read only WindowProxy document.defaultView (window of the document)
detail Read only long (float) 0.
target EventTarget (DOM element) Focused element processing the key event, root element if no suitable input element focused.
char Read only No implementado (mira error 680830) DOMString (string) 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. See key names and char values for the detail.
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.
key No implementado (mira error 680830) DOMString (string) The key value of the key represented by the event. If the value has a printed representation, this attribute's value is the same as the char attribute. Otherwise, it's one of the key value strings specified in {{ anch("Key values") }}. If the key can't be identified, this is the string "Unidentified". See key names and char values for the detail. Read Only.
charCode Read only Unsigned long (int) 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.
Warning: This attribute is deprecated; you should use char instead, if available.
keyCode Read only Unsigned long (int) A system and implementation dependent numerical code identifying the unmodified value of the pressed key. This is usually the decimal ASCII ({{ RFC(20) }}) or Windows 1252 code corresponding to the key; see {{ anch("Virtual key codes") }} for a list of common values. If the key can't be identified, this value is 0.
Warning: This attribute is deprecated; you should use key instead, if available.
which Read only Unsigned long (int) 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 key instead, if available.
location Read only long (float) The location of the key on the device.
repeat Read only No implementado boolean true if a key has been depressed long enough to trigger key repetition, otherwise false.
locale Read only No implementado string The language code for the key event, if available; otherwise, the empty string.
ctrlKey Read only boolean true if the control key was down when the event was fired. false otherwise.
shiftKey Read only boolean true if the shift key was down when the event was fired. false otherwise.
altKey Read only boolean true if the alt key was down when the event was fired. false otherwise.
metaKey Read only boolean true if the meta key was down when the event was fired. false otherwise.

preventDefault() del evento keydown

Empezando desde Gecko 25, una llamada a preventDefault() del evento keydown impide la emisión del consiguiente evento keypress. Este es un comportamiento válido para la especificación D3E y otros navegadores importantes que se comportan de esta misma forma. Por otro lado, Gecko 24 y anteriores emiten el evento keypress aunque se haya llamado a preventDefault() del evento keydown predecesor. Aunque el atributo defaultPrevented del evento keypress sea true en este caso.

Eventos relacionados

Ejemplo

<!DOCTYPE html>
<html>
<head>
<script>

'use strict';

document.addEventListener('keydown', (event) => {
  const keyName = event.key;
  alert('keydown event\n\n' + 'key: ' + keyName);
});

</script>
</head>
<body>
</body>
</html>

Etiquetas y colaboradores del documento

Etiquetas: 
 Colaboradores en esta página: juan-ferrer-toribio
 Última actualización por: juan-ferrer-toribio,