-
Notifications
You must be signed in to change notification settings - Fork 0
/
keybindings.js
38 lines (35 loc) · 899 Bytes
/
keybindings.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
32
33
34
35
36
37
38
const mappings = {
// KeyboardEvent.key: Element.class
'1': '.one',
'2': '.two',
'3': '.three',
'4': '.four',
'5': '.five',
'6': '.six',
'7': '.seven',
'8': '.eight',
'9': '.nine',
'0': '.zero',
'.': '.decimal',
'/': '.divide',
'*': '.times',
'-': '.minus',
'+': '.plus',
'=': '.equals',
'Enter': '.equals',
'Backspace': '.cancelEntry',
'Delete': '.clear',
'g': '.github-link'
};
document.addEventListener('keydown', (e) => {
if (!mappings[e.key]) return;
e.preventDefault(); // e.g. to override Firefox's Quick Find hotkey
document.querySelector(mappings[e.key]).classList.add('active');
// debugging
console.log(e.key, mappings[e.key]);
});
document.addEventListener('keyup', (e) => {
if (!mappings[e.key]) return;
document.querySelector(mappings[e.key]).classList.remove('active');
document.querySelector(mappings[e.key]).click();
});