L'événement keypress est déclenché lorsqu'une touche produisant une valeur de caractère est enfoncée. Des exemples de touches produisant une valeur de caractère sont les touches alphabétiques, numériques et de ponctuation. Des exemples de touches ne produisant pas de valeur de caractère sont des touches de modification telles que AltShiftCtrl, ou Meta.

Informations générales

Spécification
DOM L3
Interface
KeyboardEvent
Remonte
Oui
Annulable
Oui
Cible
Document, Element
Action par Défaut
Variable : évènement keypress ; lancement d'un système de composition de texte ; évènements blur etfocus ; évènement DOMActivate ; un autre évènement

Propriétés

Property Type Description
target Lecture seule EventTarget The event target (the topmost target in the DOM tree).
type Lecture seule DOMString The type of event.
bubbles Lecture seule Boolean Whether the event normally bubbles or not
cancelable Lecture seule Boolean Whether the event is cancellable or not?
view Lecture seule WindowProxy document.defaultView (window of the document)
detail Lecture seule long (float) 0.
target Lecture seule EventTarget (DOM element) Focused element processing the key event, root element if no suitable input element focused.
char Lecture seule 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 Lecture seule 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 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 Lecture seule 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 Lecture seule 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 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 Lecture seule 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 Lecture seule long (float) The location of the key on the device.
repeat Lecture seule boolean true if a key has been depressed long enough to trigger key repetition, otherwise false.
locale Lecture seule string The language code for the key event, if available; otherwise, the empty string.
ctrlKey Lecture seule boolean true if the control key was down when the event was fired. false otherwise.
shiftKey Lecture seule boolean true if the shift key was down when the event was fired. false otherwise.
altKey Lecture seule boolean true if the alt key was down when the event was fired. false otherwise.
metaKey Lecture seule boolean true if the meta key was down when the event was fired. false otherwise.

Notes

Chrome ne déclenche pas l'événement keypress pour les raccourcis clavier connus (référence). Quels raccourcis clavier sont connus dépend du système de l'utilisateur. Utilisez l'événement keydown pour implémenter les raccourcis clavier.

Evènements liés

Exemple

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

'use strict';

document.addEventListener('keypress', (event) => {
  const nomTouche = event.key;

  alert('Évènement keypress\n\n' + 'touche : ' + nomTouche);
});

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

Étiquettes et contributeurs liés au document

Contributeurs à cette page : fscholz, NemoNobobyPersonne
Dernière mise à jour par : fscholz,