audio-vis/raw/javascript/keys.js

59 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-08-07 19:31:30 +02:00
class KeyHandler {
async init() {
await this.mediaKeys();
await this.addKeyHandler();
window.addEventListener('keydown', this.keyHandler.bind(this));
}
async mediaKeys() {
if ('mediaSession' in navigator) {
let media = navigator.mediaSession;
media.setActionHandler('play', player.playStop.bind(player));
media.setActionHandler('pause', player.playStop.bind(player));
media.setActionHandler('previoustrack', player.prevSong.bind(player));
media.setActionHandler('nexttrack', player.prevSong.bind(player));
}
}
async addKeyHandler() {
eventHandler.addEvent('keys-Space', player.playStop.bind(player));
eventHandler.addEvent('keys-KeyN', player.nextSong.bind(player));
eventHandler.addEvent('keys-KeyV', player.prevSong.bind(player));
eventHandler.addEvent('keys-KeyS', playerConf.open.bind(playerConf));
eventHandler.addEvent('keys-KeyS-shift', toggleShuffle);
eventHandler.addEvent('keys-KeyB', imageUploader.renderModal.bind(imageUploader));
eventHandler.addEvent('keys-KeyF-shift', forceAllRead);
eventHandler.addEvent('keys-KeyH', gui.openHelp);
eventHandler.addEvent('keys-KeyP', e => {
player.playlist.renderPagination(player.playlist.page);
gui.modal.showModal();
});
eventHandler.addEvent('keys-Escape, keys-KeyC-shift', e => {
gui.modal.resetModal();
gui.modal.closeModal();
})
eventHandler.addEvent('keys-F11', e => {
if (document.fullscreenElement) {
document.exitFullscreen().catch(console.error);
} else {
document.body.requestFullscreen().catch(console.error);
}
});
}
async keyHandler(event) {
let key = event.code,
shift = event.shiftKey ? '-shift' : '',
ctrl = event.ctrlKey ? '-ctrl' : '',
name = 'keys-' + key + shift + ctrl;
if (eventHandler.handleEvent({
data: {
cmd: name
}
})) {
event.preventDefault();
event.stopPropagation();
}
}
}