-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeyPressed.html
More file actions
46 lines (33 loc) · 1.23 KB
/
keyPressed.html
File metadata and controls
46 lines (33 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE HTML>
<html>
<body>
<p>Нажмите "Q" и "W" вместе (язык значения не играет).</p>
<script>
function runOnKeys(func, ...codes) {
let pressed = new Set();
document.addEventListener('keydown', function (event) {
pressed.add(event.code);
for (let code of codes) { // все ли клавиши из набора нажаты?
if (!pressed.has(code)) {
return;
}
}
// да, все
// во время показа alert, если посетитель отпустит клавиши - не возникнет keyup
// при этом JavaScript "пропустит" факт отпускания клавиш, а pressed[keyCode] останется true
// чтобы избежать "залипания" клавиши -- обнуляем статус всех клавиш, пусть нажимает всё заново
pressed.clear();
func();
});
document.addEventListener('keyup', function (event) {
pressed.delete(event.code);
});
}
runOnKeys(
() => alert("Привет!"),
"KeyQ",
"KeyW"
);
</script>
</body>
</html>