Esempio
Prendi il tasto della tastiera che è stato premuto quando un evento chiave è verificato:
var x = event.key;
Il risultato di x potrebbe essere:
Enter
Prova tu stesso " Più "Try it Yourself" esempi di seguito.
Definizione e l'utilizzo
La proprietà chiave restituisce l'identificativo del tasto premuto quando un evento chiave è verificato.
identificatori principali sono stringhe che identificano i tasti della tastiera. Il valore di ritorno di questa proprietà può essere una stringa di:
- Un singolo carattere (like "a" , "W" , "4" , "+" or "$")
- Un multicarattere (like "F1" , "Enter" , "HOME" or "CAPS LOCK")
Nota: questa proprietà è di sola lettura.
Suggerimento: Se si vuole scoprire se la "ALT" , "CTRL" , "META" o "SHIFT" tasto è stato premuto quando un evento chiave è verificato, utilizzare l'altKey , ctrlKey , metakey o shiftKey proprietà.
Supporto browser
I numeri nella tabella indicano la prima versione del browser che supporta pienamente la proprietà.
Proprietà | |||||
---|---|---|---|---|---|
key | Non supportato | 9.0 | 23.0 | Non supportato | Non supportato |
Sintassi
event .key
Dettagli tecnici
Valore di ritorno: | A String, che rappresenta il tasto della tastiera premuto. Valori possibili:
|
---|---|
DOM Versione: | Livello 3 DOM Eventi |

Altri esempi
Esempio
Avvertire un testo se l'utente preme il "A" chiave:
var x = event.key;
// If the pressed keyboard
button is "a" or "A" (using caps lock or shift), alert some text.
if
(x == "a" || x == "A") {
alert
("You pressed the 'A' key!");
}
Prova tu stesso " Pagine correlate
Di riferimento HTML DOM: KeyboardEvent keyCode Property
Di riferimento HTML DOM: KeyboardEvent which Property
Di riferimento HTML DOM: KeyboardEvent charCode Property
<Oggetto evento