Skip to content

Keyboard: keydown and keyup #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

We should use two handlers: `document.onkeydown` and `document.onkeyup`.
Qui dobbiamo usare due gestori: `document.onkeydown` e `document.onkeyup`.

Let's create a set `pressed = new Set()` to keep currently pressed keys.
Andiamo ad impostare `pressed = new Set()` per memorizzare i tasti attualmente premuti.

The first handler adds to it, while the second one removes from it. Every time on `keydown` we check if we have enough keys pressed, and run the function if it is so.
Il primo gestore lo aggiunge, mentre il secondo lo rimuove. Ad ogni `keydown` controlliamo se abbiamo abbastanza tasti premuti, ed in caso affermativo la funzione verrà eseguita.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<body>

<p>Press "Q" and "W" together (can be in any language).</p>
<p>Premere "Q" e "W" contemporaneamente (può essere in qualunque lingua).</p>

<script>
function runOnKeys(func, ...codes) {
Expand All @@ -11,19 +11,19 @@
document.addEventListener('keydown', function(event) {
pressed.add(event.code);

for (let code of codes) { // are all keys in the set?
for (let code of codes) { // le chiavi sono tutte contenute nel Set?
if (!pressed.has(code)) {
return;
}
}

// yes, they are
// sì, lo sono

// during the alert, if the visitor releases the keys,
// JavaScript does not get the "keyup" event
// and pressed set will keep assuming that the key is pressed
// so, to evade "sticky" keys, we reset the status
// if the user wants to run the hotkey again - let them press all keys again
// durante l'alert, se il visitatore rilascia i tasti,
// JavaScript non cattura l'evento "keyup"
// ed il set premuto verrà mantenuto, credendo che i tasti siano ancora premuti
// quindi, per evitare tasti "incollati", resettiamo lo status
// se l'utente vuole eseguire di nuovo la scorciatoia, premerà nuovamente
pressed.clear();

func();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Extended hotkeys
# Tasti di scelta rapida estesi

Create a function `runOnKeys(func, code1, code2, ... code_n)` that runs `func` on simultaneous pressing of keys with codes `code1`, `code2`, ..., `code_n`.
Create una funzione `runOnKeys(func, code1, code2, ... code_n)` che esegue `func` quando vengono premuti contemporaneamente i tasti con i codici `code1`, `code2`, ..., `code_n`.

For instance, the code below shows `alert` when `"Q"` and `"W"` are pressed together (in any language, with or without CapsLock)
Ad esempio, il seguente codice mostra un `alert` quando vengono premuti `"Q"` e `"W"` insieme (in qualunque lingua, con o senza il CapsLock)

```js no-beautify
runOnKeys(
Expand Down
Loading