the-glorious-startpage/js/keybindings.js

81 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-06-04 09:09:22 +02:00
let keysPressed = {};
document.addEventListener(
'keydown',
(event) => {
2020-06-04 09:18:32 +02:00
keysPressed[event.key] = true;
2020-06-04 09:09:22 +02:00
// Open dashboard
if (keysPressed['Alt'] && event.key === 's') {
2020-06-04 09:18:32 +02:00
event.preventDefault();
2020-06-04 09:09:22 +02:00
toggleDashboard();
return;
} else if (keysPressed['Alt'] && event.key === 'e') {
2020-06-04 09:18:32 +02:00
event.preventDefault();
2020-06-04 09:09:22 +02:00
toggleWebMenu();
return;
} else if (keysPressed['Alt'] && event.key === 'x') {
2020-06-04 09:18:32 +02:00
event.preventDefault();
2020-06-04 09:09:22 +02:00
toggleWeatherScreen();
return;
}
2020-06-04 09:18:32 +02:00
if (event.key === 'Escape') {
2020-06-05 04:22:20 +02:00
event.preventDefault();
2020-06-04 09:18:32 +02:00
// If any of this are open, close it
2020-06-04 09:18:32 +02:00
if (searchBoxVisility) {
2020-06-05 04:22:20 +02:00
2020-06-04 09:18:32 +02:00
// Hide searchbox
toggleSearchBox();
searchBox.value = '';
return;
2020-06-05 04:22:20 +02:00
} else if (weatherScreenVisibility) {
2020-06-05 04:22:20 +02:00
toggleWeatherScreen();
return;
2020-06-05 04:22:20 +02:00
} else if (rightDashboardVisibility) {
2020-06-05 04:22:20 +02:00
toggleDashboard();
return;
2020-06-05 04:22:20 +02:00
}
2020-06-04 09:18:32 +02:00
// Show web menu
toggleWebMenu();
return;
}
if (searchBoxVisility === false) {
// Don't show searchbox when web menu, dashboard
// and weather screen is open
if (webMenuVisibility || weatherScreenVisibility ||
rightDashboardVisibility) return;
2020-06-05 04:22:20 +02:00
// Only accept single charactered key and backspace to open search box
if ((event.key.length > 1) && (event.key !== 'Backspace')) return;
2020-06-04 09:18:32 +02:00
// Open searchbox
toggleSearchBox();
} else {
// Backspacing while there's no search query will hide searhbox
// Will also hide if you hit enter
if ((event.key === 'Backspace' || event.key === 'Enter') &&
searchBox.value < 1) { toggleSearchBox(); return; };
}
2020-06-04 09:09:22 +02:00
}
);
document.addEventListener(
'keyup',
(event) => {
delete keysPressed[event.key];
}
);