From bb4e8dfa346275133ef5bf19f32f69340a338328 Mon Sep 17 00:00:00 2001 From: nklhrstv Date: Tue, 17 Mar 2020 13:50:31 +0200 Subject: [PATCH] KeyboardNavigation refactored to work with event.code --- .../KeyboardNavigation/KeyboardNavigation.js | 59 ++++++++++++------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/src/services/KeyboardNavigation/KeyboardNavigation.js b/src/services/KeyboardNavigation/KeyboardNavigation.js index 8af8a7f85..80fd7cc31 100644 --- a/src/services/KeyboardNavigation/KeyboardNavigation.js +++ b/src/services/KeyboardNavigation/KeyboardNavigation.js @@ -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; } } }