You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/7-keyboard-events/article.md
+41-41Lines changed: 41 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,83 +1,83 @@
1
-
# Keyboard: keydown and keyup
1
+
# Tastiera: keydown e keyup
2
2
3
-
Before we get to keyboard, please note that on modern devices there are other ways to "input something". For instance, people use speech recognition (especially on mobile devices) or copy/paste with the mouse.
3
+
Prima di andare alla tastiera, è bene ricordare che nei dispositivi moderni esistono altre maniere per "inserire qualche dato". Per esempio citiamo l'uso del riconoscimento vocale (specialmente sui dispositivi mobile) o il copia/incolla con il mouse.
4
4
5
-
So if we want to track any input into an `<input>` field, then keyboard events are not enough. There's another event named `input`to track changes of an `<input>` field, by any means. And it may be a better choice for such task. We'll cover it later in the chapter<info:events-change-input>.
5
+
Quindi se vogliamo tenere traccia di qualunque input dentro un campo `<input>`, allora gli eventi della tastiera non saranno sufficienti. Esite però un altro evento chiamato `input`per tenere traccia delle modifiche su un `<input>`, di qualunque natura. E questa potrebbe essere la scelta corretta per qursto tipo di attività. Verrà affrontato più avanti nel capitolo<info:events-change-input>.
6
6
7
-
Keyboard events should be used when we want to handle keyboard actions (virtual keyboard also counts). For instance, to react on arrow keys`key:Up`and`key:Down`or hotkeys (including combinations of keys).
7
+
Gli eventi della tastiera dovrebbero essere usati per gestire azioni da tastiera (comprese le tastiere virtuali). Per esempio, per reagire ai tasti freccia`key:Up`e`key:Down`oppure alle scorciatoie (includendo quindi combinazioni di tasti).
8
8
9
9
10
-
## Teststand[#keyboard-test-stand]
10
+
## Banco di test[#keyboard-test-stand]
11
11
12
12
```offline
13
-
To better understand keyboard events, you can use the [teststand](sandbox:keyboard-dump).
13
+
Per capire meglio gli eventi da tastiera, possiamo usare il [banco di test](sandbox:keyboard-dump).
14
14
```
15
15
16
16
```online
17
-
To better understand keyboard events, you can use the teststand below.
17
+
Per capire meglio gli eventi da tastiera, possiamo usare il seguente banco di test.
18
18
19
-
Try different key combinations in the text field.
19
+
Proviamo diverse combinazioni di tasti nel campo di testo.
20
20
21
21
[codetabs src="keyboard-dump" height=480]
22
22
```
23
23
24
24
25
-
## Keydown and keyup
25
+
## Keydown e keyup
26
26
27
-
The`keydown`events happens when a key is pressed down, and then `keyup`-- when it's released.
27
+
L'evento`keydown`avviene quando viene premuto un tasto, e `keyup`quando viene rilasciato.
28
28
29
-
### event.code and event.key
29
+
### event.code e event.key
30
30
31
-
The `key`property of the event object allows to get the character, while the `code`property of the event object allows to get the "physical key code".
31
+
La proprietà `key`dell'oggetto evento, permette di ottenere il carattere, mentre la proprietà `code`ci restituisce "codice del tasto fisico".
32
32
33
-
For instance, the same key`key:Z`can be pressed with or without `key:Shift`. That gives us two different characters: lowercase`z`and uppercase`Z`.
33
+
Ad esempio, a parità di`key:Z`potrebbe essere stato premuto con o senza il `key:Shift`. Questo potrebbe darci due differenti caratteri: minuscolo`z`e maiuscolo`Z`.
34
34
35
-
The `event.key`is exactly the character, and it will be different. But`event.code`is the same:
35
+
`event.key`è esattamente il carattere, e sarà differente. Invece`event.code`è sempre lo stesso:
36
36
37
37
| Key |`event.key`|`event.code`|
38
38
|--------------|-------------|--------------|
39
-
|`key:Z`|`z` (lowercase) |`KeyZ`|
40
-
|`key:Shift+Z`|`Z` (uppercase) |`KeyZ`|
39
+
|`key:Z`|`z` (minuscolo) |`KeyZ`|
40
+
|`key:Shift+Z`|`Z` (maiuscolo) |`KeyZ`|
41
41
42
42
43
-
If a user works with different languages, then switching to another language would make a totally different character instead of `"Z"`. That will become the value of `event.key`, while`event.code`is always the same:`"KeyZ"`.
43
+
Se un utente fa uso di diverse lingue, passare ad un'altra significherebbe avere tutt'altro carattere rispetto a `"Z"`. Quest'ultimo diverrebbe il valore di `event.key`, mentre`event.code`sarebbe sempre`"KeyZ"`.
44
44
45
-
```smart header="\"KeyZ\"and other key codes"
46
-
Every key has the code that depends on its location on the keyboard. Key codes described in the [UI Events code specification](https://www.w3.org/TR/uievents-code/).
45
+
```smart header="\"KeyZ\"e altri codici tasto"
46
+
Ogni tasto ha un codice che dipende dalla sua posizione sulla tastiera. I codici dei tasti vengono descritti nelle [specifiche dei codici Evento UI](https://www.w3.org/TR/uievents-code/).
47
47
48
-
For instance:
49
-
- Letter keys have codes `"Key<letter>"`: `"KeyA"`, `"KeyB"` etc.
50
-
- Digit keys have codes: `"Digit<number>"`: `"Digit0"`, `"Digit1"` etc.
51
-
- Special keys are coded by their names: `"Enter"`, `"Backspace"`, `"Tab"` etc.
48
+
Per esempio:
49
+
- I tasti lettera hanno dei codici: `"Key<letter>"`: `"KeyA"`, `"KeyB"` etc.
50
+
- I tasti numerici hanno dei codici: `"Digit<number>"`: `"Digit0"`, `"Digit1"` etc.
51
+
- I tasti speciali sono codificati con i loro nomi: `"Enter"`, `"Backspace"`, `"Tab"` etc.
52
52
53
-
There are several widespread keyboard layouts, and the specification gives key codes for each of them.
53
+
Esiste una grande varietà di layout di tastiera, e le specifiche danno un codice per ognuno di essi.
54
54
55
-
Read the [alphanumeric section of the spec](https://www.w3.org/TR/uievents-code/#key-alphanumeric-section) for more codes, or just press a key in the [teststand](#keyboard-test-stand) above.
55
+
Per avere informazioni sui vari codici [sezione alfanumerica delle specifiche](https://www.w3.org/TR/uievents-code/#key-alphanumeric-section), oppure basta premere un tasto nel [banco di test](#keyboard-test-stand) precedente.
```warn header="La distinzione tra maiuscolo e minuscolo è importante: `\"KeyZ\"`, e non`\"keyZ\"`"
59
+
Sembra ovvio, ma le persone sbagliano ancora.
60
60
61
-
Please evade mistypes: it's`KeyZ`, not `keyZ`. The check like `event.code=="keyZ"`won't work: the first letter of`"Key"`must be uppercase.
61
+
Bisogna evitare di scrivere in modo scorretto: è`KeyZ`, e non `keyZ`. Un controllo fatto in questo modo `event.code=="keyZ"`non funziona: la prima lettera di`"Key"`deve essere maiuscaola.
62
62
```
63
63
64
-
What if a key does not give any character? For instance, `key:Shift` or `key:F1` or others. For those keys, `event.key` is approximately the same as `event.code`:
64
+
Cosa succederebbe se un tasto non restituisse nessun carattere? Per esempio, `key:Shift` oppure `key:F1` o altri ancora. Per questi tasti il valore di `event.key` è approssiamtivamente lo stesso di `event.code`:
65
65
66
66
| Key | `event.key` | `event.code` |
67
67
|--------------|-------------|--------------|
68
68
| `key:F1` |`F1` |`F1` |
69
69
| `key:Backspace` |`Backspace` |`Backspace` |
70
70
| `key:Shift`|`Shift` |`ShiftRight` or `ShiftLeft` |
71
71
72
-
Please note that `event.code` specifies exactly which key is pressed. For instance, most keyboards have two `key:Shift` keys: on the left and on the right side. The `event.code` tells us exactly which one was pressed, and `event.key` is responsible for the "meaning" of the key: what it is (a "Shift").
72
+
Nota bene che `event.code` specifica esattamente il tasto premuto. Per esempio, la maggioranza delle tastiere hanno due tasti `key:Shift`: uno nel lato sinistro e uno nel lato destro. `event.code` ci dice esattamente quale viene premuto, ed `event.key` è responsable invece del "significato" del tasto: cosa è (uno "Shift").
73
73
74
-
Let's say, we want to handle a hotkey: `key:Ctrl+Z` (or `key:Cmd+Z` for Mac). Most text editors hook the "Undo" action on it. We can set a listener on `keydown` and check which key is pressed.
74
+
Mettiamo il caso che volessimo gestire una scorciatoia: `key:Ctrl+Z` (o `key:Cmd+Z` su Mac). La maggiorparte degli editor di testo aggancia su di esso l'azione "Undo". Possiamo impostare un listener sul `keydown` e controllare quale tasto viene premuto.
75
75
76
-
There's a dilemma here: in such a listener, should we check the value of `event.key` or `event.code`?
76
+
Ma qui c'è un dilemma: in questo listener, dovremmo controllare il valore di `event.key` oppure quello di `event.code`?
77
77
78
-
On one hand, the value of `event.key` is a character, it changes depending on the language. If the visitor has several languages in OS and switches between them, the same key gives different characters. So it makes sense to check `event.code`, it's always the same.
78
+
Da una parte, il valore di `event.key` è un carattere, e cambia a seconda del linguaggio. Se il visitatore ha più lingue nel suo sistema operativo e passa da uno all'altro, lo stesso tasto restituirà caratteri differenti. Quindi ha senso controllare `event.code`, che è sempre lo stesso.
On the other hand, there's a problem with`event.code`. For different keyboard layouts, the same key may have different characters.
90
+
D'altra parte, c'è un problema con`event.code`. Per layout di tastiera differenti, possono corrispondere caratteri differenti.
91
91
92
-
For example, here are US layout ("QWERTY") and German layout ("QWERTZ") under it (from Wikipedia):
92
+
Per esempio, qui abbiamo un layout americano ("QWERTY") ed un layout Tedesco ("QWERTZ") sotto di esso (da Wikipedia):
93
93
94
94

95
95
96
96

97
97
98
-
For the same key, US layout has "Z", while German layout has "Y" (letters are swapped).
98
+
A parità di tasto, sul layout americano corrisponde a "Z", mentre per quello tedesco corrisponde a "Y" (le lettere sono scambiate tra di loro).
99
99
100
-
Literally, `event.code`will equal`KeyZ`for people with German layout when they press`key:Y`.
100
+
Letteralmente, `event.code`equivale a`KeyZ`per gli utenti con il layout tedesco quando premono`key:Y`.
101
101
102
-
If we check `event.code == 'KeyZ'`in our code, then for people with German layout such test will pass when they press`key:Y`.
102
+
Se andiamo a controllare `event.code == 'KeyZ'`nel nostro codice, per gli utenti con il layout tedesco, il test passerà alla pressione del tasto`key:Y`.
103
103
104
-
That sounds really odd, but so it is. The [specification](https://www.w3.org/TR/uievents-code/#table-key-code-alphanumeric-writing-system)explicitly mentions such behavior.
104
+
Questo può sembrare strano,ma è così. Le [specifiche](https://www.w3.org/TR/uievents-code/#table-key-code-alphanumeric-writing-system)menzionano in modo esplicito questo comportamento.
105
105
106
-
So, `event.code`may match a wrong character for unexpected layout. Same letters in different layouts may map to different physical keys, leading to different codes. Luckily, that happens only with several codes, e.g. `keyA`, `keyQ`, `keyZ` (as we've seen), and doesn't happen with special keys such as `Shift`. You can find the list in the [specification](https://www.w3.org/TR/uievents-code/#table-key-code-alphanumeric-writing-system).
106
+
Quindi, `event.code`può corrispondere a un carattere errato da layout inaspettati. A parità di lettera, per layout differenti potrebbero essere mappati a tasti fisici differenti, portando a codici differenti. Fortunatamente, that happens only with several codes, e.g. `keyA`, `keyQ`, `keyZ` (as we've seen), and doesn't happen with special keys such as `Shift`. You can find the list in the [specification](https://www.w3.org/TR/uievents-code/#table-key-code-alphanumeric-writing-system).
107
107
108
108
To reliably track layout-dependent characters, `event.key` may be a better way.
0 commit comments