the-glorious-startpage/js/keybindings.js

81 lines
2.3 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') {
// If any of this are open, close it
2020-06-04 09:18:32 +02:00
if (searchBoxVisility) {
// Hide searchbox
toggleSearchBox();
searchBox.value = '';
return;
} else if (weatherScreenVisibility) {
toggleWeatherScreen();
return;
} else if (rightDashboardVisibility) {
toggleDashboard();
return;
}
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;
// Dont accept ctrl, alt,
// left window key, f5, f12, return
if (event.key === 'Control' ||
event.key === 'Alt' ||
event.key === 'Shift' ||
event.key === 'Meta' ||
event.key === 'F5' ||
event.key === 'F12' ||
event.key === 'Enter')
return;
// 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];
}
);