Keyboard: keydown and keyup by OlhaBrozhenets · Pull Request #293 · javascript-tutorial/uk.javascript.info
Expand Up
@@ -2,7 +2,7 @@
<html>
<body>
<p>Press "Q" and "W" together (can be in any language).</p> <p>Натисніть "Q" і "W" разом (можна будь-якою мовою).</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) { // чи всі клавіші в наборі? if (!pressed.has(code)) { return; } }
// yes, they are // так
// 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 // під час оповіщення, якщо відвідувач відпускає клавіші, // JavaScript не отримує подію "keyup" // і набір натиснутих клавіш продовжуватиме вважати, що клавіша натиснута // отже, щоб уникнути «липких» клавіш, ми скидаємо статус // якщо користувач хоче знову запустити гарячу клавішу -- дозвольте йому знову натиснути всі клавіші pressed.clear();
func(); Expand All @@ -36,7 +36,7 @@ }
runOnKeys( () => alert("Hello!"), () => alert("Привіт!"), "KeyQ", "KeyW" ); Expand Down
<p>Press "Q" and "W" together (can be in any language).</p> <p>Натисніть "Q" і "W" разом (можна будь-якою мовою).</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) { // чи всі клавіші в наборі? if (!pressed.has(code)) { return; } }
// yes, they are // так
// 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 // під час оповіщення, якщо відвідувач відпускає клавіші, // JavaScript не отримує подію "keyup" // і набір натиснутих клавіш продовжуватиме вважати, що клавіша натиснута // отже, щоб уникнути «липких» клавіш, ми скидаємо статус // якщо користувач хоче знову запустити гарячу клавішу -- дозвольте йому знову натиснути всі клавіші pressed.clear();
func(); Expand All @@ -36,7 +36,7 @@ }
runOnKeys( () => alert("Hello!"), () => alert("Привіт!"), "KeyQ", "KeyW" ); Expand Down