Skip to content

Commit

Permalink
Emscripten: Don't swallow browser keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
jdarpinian committed Jun 22, 2024
1 parent c1ab47a commit 031bbd0
Showing 1 changed file with 12 additions and 0 deletions.
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

0 comments on commit 031bbd0

Please sign in to comment.