-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (37 loc) · 1.03 KB
/
script.js
File metadata and controls
45 lines (37 loc) · 1.03 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
let lastKey = null;
const PROP_NAMES = ['ctrlKey', 'shiftKey', 'altKey', 'metaKey', 'code', 'key', 'charCode', 'keyCode'];
function isModifier(key) {
return PROP_NAMES.slice(0, 4).includes(key);
}
function isMain(key) {
return PROP_NAMES.slice(4, 6).includes(key);
}
function isDeprecated(key) {
return PROP_NAMES.slice(-2).includes(key);
}
/**
*
* @param {KeyboardEvent} e
*/
function handlePress(e) {
e.preventDefault();
if (e.code === lastKey) return;
const values = PROP_NAMES.map(prop => [prop, e[prop]]);
lastKey = e.code;
values.map(([key, val]) => {
document.querySelector(`#${key}`).innerText = isMain(key) ? `"${val}"` : `${val}`;
if (isModifier(key)) {
document.querySelector(`#${key}`).dataset.active = val;
}
});
}
function buildPage() {
const input = document.querySelector('input');
input.addEventListener('keydown', handlePress);
input.addEventListener('blur', () => {
setTimeout(() => {
input.focus();
}, 0);
});
}
document.addEventListener('DOMContentLoaded', buildPage);