-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
31 lines (29 loc) · 982 Bytes
/
script.js
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
const $key = function (key) { return (document.querySelector("kbd[data-key='" + key + "'], kbd[data-alt='" + key + "']")); };
const codeToElement = {
'CapsLock': $key('caps'),
'Space': $key('space'),
'Backslash': document.getElementById('backslash'),
'Quote': document.getElementById('quote'),
'ShiftLeft': $key('lshift'),
'ShiftRight': $key('rshift'),
'ControlLeft': $key('lctrl'),
'ControlRight': $key('rctrl'),
'AltLeft': $key('lalt'),
'AltRight': $key('ralt'),
'MetaLeft': $key('lwin'),
'MetaRight': $key('rwin')
};
window.addEventListener('keydown', function (e) {
const el = codeToElement[e.code] || $key(e.key.toLowerCase());
if (el) {
el.classList.add('pressed');
e.preventDefault();
}
});
window.addEventListener('keyup', function (e) {
const el = codeToElement[e.code] || $key(e.key.toLowerCase());
if (el) {
el.classList.remove('pressed');
e.preventDefault();
}
});