fix browser scroll by consuming arrow press events

This commit is contained in:
2026-11-21 15:32:41 +03:00
parent 1bdcd4ada1
commit 24cedeef9d

View File

@@ -9,6 +9,7 @@
html, body { html, body {
margin: 0; margin: 0;
height: 100%; height: 100%;
overflow: hidden;
background: #05070d; background: #05070d;
color: #7c8798; color: #7c8798;
font: 14px/1.5 system-ui, sans-serif; font: 14px/1.5 system-ui, sans-serif;
@@ -58,6 +59,19 @@
}; };
unlockEvents.forEach((type) => addEventListener(type, unlockAudio, true)); unlockEvents.forEach((type) => addEventListener(type, unlockAudio, true));
// Arrow keys and Space also scroll the page by default. Cancel that
// while the game canvas has focus, but keep modified keys for browser
// shortcuts.
const gameCanvas = document.getElementById('canvas');
const scrollKeys = new Set([
'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Space',
]);
addEventListener('keydown', (event) => {
if (event.ctrlKey || event.metaKey || event.altKey) return;
if (event.target !== gameCanvas && document.activeElement !== gameCanvas) return;
if (scrollKeys.has(event.code)) event.preventDefault();
}, { capture: true });
var Module = { var Module = {
canvas: document.getElementById('canvas'), canvas: document.getElementById('canvas'),
print: console.log.bind(console), print: console.log.bind(console),