-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.js
38 lines (35 loc) · 868 Bytes
/
keys.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 SHORTCUTS = [{
key: 'n',
action: player => player.playNext()
}, {
key: 'p',
action: player => player.playPrevious()
}, {
key: ' ',
action: player => {
if (player.isPlaying) {
player.pause();
} else {
player.play();
}
}
}, {
key: 'v',
action: (_, toggleVisualizer) => toggleVisualizer()
}];
function getShortcut(key) {
return SHORTCUTS.find(shortcut => shortcut.key === key);
}
export function initKeyboardShortcuts(player, toggleVisualizer) {
addEventListener('keydown', e => {
// If space was used but a button was focused, we're only
// activating the button, not executing a shortcut.
if (e.key === ' ' && e.target.tagName.toLowerCase() === 'button') {
return;
}
const shortcut = getShortcut(e.key);
if (shortcut) {
shortcut.action(player, toggleVisualizer);
}
});
}