Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Keyboard: keydown and keyup#293

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@

We should use two handlers: `document.onkeydown`and `document.onkeyup`.
Ми повинні використовувати два обробники: `document.onkeydown`і `document.onkeyup`.

Let's create a set`pressed = new Set()` to keep currently pressed keys.
Давайте створимо набір`pressed = new Set()`, щоб зберегти поточні натиснуті клавіші.

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.
Перший обробник додає до нього, а другий видаляє. Кожного разу при `keydown`ми перевіряємо, чи достатньо натиснутих клавіш, і запускаємо функцію, якщо це так.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
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,
// JavaScriptdoes 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,15 +2,15 @@ importance: 5

---

#Extended hotkeys
#Розширені комбінації гарячих клавіш

Create a function`runOnKeys(func, code1, code2, ... code_n)` that runs `func`on simultaneous pressing of keys with codes `code1`, `code2`, ..., `code_n`.
Створіть функцію`runOnKeys(func, code1, code2, ... code_n)`, яка запускає `func`при одночасному натисканні клавіш із кодами `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)
Наприклад, код нижче показує`alert`, коли `"Q"`та `"W"`натискаються разом (будь-якою мовою, з або без CapsLock)

```js no-beautify
runOnKeys(
() => alert("Hello!"),
() => alert("Привіт!"),
"KeyQ",
"KeyW"
);
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp