Skip to content
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`.
Debemos manejar dos eventos: `document.onkeydown` y `document.onkeyup`.

Let's create a set `pressed = new Set()` to keep currently pressed keys.
Creemos un set `pressed = new Set()` para registrar las teclas presionads actualmente.

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.
El primer manejador las agrega en él, mientras que el segundo las quita. Con cada `keydown` verificamos si tenemos suficientes teclas presionadas, y ejecutamos la función si es así.
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>Presione "Q" y "W" juntas (puede ser en cualquier lenguaje).</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) { // ¿están todas las teclas en el set?
if (!pressed.has(code)) {
return;
}
}

// yes, they are
// sí, lo están

// 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 el alert, si el visitante suelta las teclas,
// JavaScript no obtiene el evento "keyup"
// y el set "pressed" las mantendrá asumiendo que las teclas siguen presionadas;
// por ello, para evitar teclas "pegadas", reseteamos el estado
// si el usuario quiere usa el atajo de nuevo, permitámoslo
pressed.clear();

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

---

# Extended hotkeys
# Extendiendo atajos de teclado

Create a function `runOnKeys(func, code1, code2, ... code_n)` that runs `func` on simultaneous pressing of keys with codes `code1`, `code2`, ..., `code_n`.
Crea una función `runOnKeys(func, code1, code2, ... code_n)` que ejecute `func` al presionar simultáneamente las teclas con códigos `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)
Por ejemplo, el siguiente código muestra un `alert` cuando `"Q"` y `"W"` se presionan juntas (en cualquier lenguaje, con o sin mayúscula)

```js no-beautify
runOnKeys(
() => alert("Hello!"),
() => alert("¡Hola!"),
"KeyQ",
"KeyW"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<form id="form" onsubmit="return false">

Prevent default for:
Evitar el predeterminado (prevent default) para:
<label>
<input type="checkbox" name="keydownStop" value="1"> keydown</label>&nbsp;&nbsp;&nbsp;
<label>
Expand All @@ -24,7 +24,7 @@
<input type="checkbox" name="keyupIgnore" value="1"> keyup</label>
</p>

<p>Focus on the input field and press a key.</p>
<p>Haz foco en el campo input y presiona una tecla.</p>

<input type="text" placeholder="Press keys here" id="kinput">

Expand Down