KeyboardNavigation refactored to work with event.code

This commit is contained in:
nklhrstv 2020-03-17 13:50:31 +02:00
parent dd37b621c3
commit bb4e8dfa34

View file

@ -1,32 +1,51 @@
const TABS = [
{ href: '#/', key: 'F1' },
{ href: '#/discover', key: 'F2' },
{ href: '#/library', key: 'F3' }
];
function KeyboardNavigation() {
let active = false;
function onKeyDown(event) {
const tab = TABS.find(({ key }) => key === event.key);
if (tab) {
event.preventDefault();
window.location = tab.href;
if (event.keyboardNavigationPrevented) {
return;
}
if (event.target.tagName !== 'INPUT') {
if (event.key === 'Backspace') {
window.history.back();
return;
switch (event.code) {
case 'Digit1': {
window.location = '#/';
break;
}
}
case 'Digit2': {
window.location = '#/discover';
break;
}
case 'Digit3': {
window.location = '#/library';
break;
}
case 'Digit4': {
window.location = '#/settings';
break;
}
case 'Digit5': {
window.location = '#/addons';
break;
}
case 'Backspace': {
if (event.target.tagName !== 'INPUT') {
if (event.ctrlKey) {
window.history.forward();
} else {
window.history.back();
}
}
if (event.code === 'KeyF') {
if (document.fullscreenElement === document.documentElement) {
document.exitFullscreen();
} else {
document.documentElement.requestFullscreen();
break;
}
case 'KeyF': {
if (document.fullscreenElement === document.documentElement) {
document.exitFullscreen();
} else {
document.documentElement.requestFullscreen();
}
break;
}
}
}