Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emscripten: Don't swallow browser keyboard shortcuts #674

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions code/web/client.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
<canvas id=canvas></canvas>

<script type=module>

const defaultKeyBindingKeyCodes = ["KeyW", "KeyA", "KeyS", "KeyD", "KeyC", "KeyT", "Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6", "Digit7", "Digit8", "Digit9", "Tab", "Space", "Enter", "NumpadEnter", "Delete", "Slash", "Backslash", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "PageDown", "End", "Escape", "ControlLeft", "ControlRight", "ShiftLeft", "ShiftRight", "AltLeft", "AltRight",];
const defaultKeyBindingKeyCodesMap = defaultKeyBindingKeyCodes.reduce((acc, code) => { acc[code] = true; return acc; }, {});
window.addEventListener("keydown", (e) => {
// Emscripten SDL2 will preventDefault all keyboard events which prevents browser keyboard shortcuts from working.
// This was supposed to be fixed in https://github.com/emscripten-core/emscripten/issues/16462 however the fix regressed.
// This hack lets the browser handle everything, except for the default Quake III keybindings. When the above mentioned
// regression is fixed, this can be replaced with a call to SDL_SetEventFilter in sdl_input.c respecting the actual
// active keybindings instead of hardcoding them here.
if (!defaultKeyBindingKeyCodesMap[e.code]) e.preventDefault = () => false;
}, { capture: true });

// These strings are set in the generated HTML file in the build directory.
let CLIENTBIN = '__CLIENTBIN__';
let BASEGAME = '__BASEGAME__';
Expand Down