Closed zippoxer closed 4 years ago
Ah, we have a list of "navigation" keys that we always prevent default on, adding F5 (116?) there should help.
As a quick check, you can disable TEXTINPUT
events (SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
), which prevents default on everything.
Ah, we have a list of "navigation" keys that we always prevent default on, adding F5 (116?) there should help.
As a quick check, you can disable
TEXTINPUT
events (SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
), which prevents default on everything.
Thanks for the quick response :-)
I'm now calling SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE)
right after SDL_CreateWindow
(in the C), and it does prevent default on everything (F5 as well), but the issue now is that the game doesn't receive any text input as well.
I guess that's the expected behaviour with SDL_EventState
, no?
Can't I preventDefault
all keyboard events (as far as the browser allows obviously) and still have the game receive them through the canvas? Essentially, I want to "trap" keyboard events in the game from bubbling up to the browser.
OK, I'm stupid. I can just do this:
window.onkeydown = function (event) {
// F1-F12
if (event.keyCode >= 112 && event.keyCode <= 123) {
event.preventDefault();
}
}
And the game still receives F1-F12 even with preventDefault. Yay!
For some reason, the game doesn't receive text keys when you preventDefault them in the same way. That's interesting, is this a limitation with Emscripten's SDL port or is it just how browsers work?
If anyone needs it:
function shouldPreventKey(event) {
var code = event.which || event.keyCode;
if (event.ctrlKey) {
return true;
}
// F1-F12
if (code >= 112 && code <= 123) {
return true;
}
}
window.onkeydown = function (event) {
if (shouldPreventKey(event)) {
event.preventDefault();
}
}
It's a browser thing, preventing the default on a keydown
event prevents the keypress
event from being sent, which is what we use for SDL_TEXTINPUT
.
Usually, any SDL event that is enabled passes it through to SDL and prevents the default action. keydown
events have special handling to still allow keypress (unless SDL_TEXTINPUT
is disabled) with that little blacklist of "bad keys". (Which I should add the F-keys to).
It's a browser thing, preventing the default on a
keydown
event prevents thekeypress
event from being sent, which is what we use forSDL_TEXTINPUT
.Usually, any SDL event that is enabled passes it through to SDL and prevents the default action.
keydown
events have special handling to still allow keypress (unlessSDL_TEXTINPUT
is disabled) with that little blacklist of "bad keys". (Which I should add the F-keys to).
Thanks, sounds great. I think your defaults are great for most applications. In my search, I haven't seen many issues like mine. Maybe an ENV to toggle "desktop mode" for applications like mine would be more approriate? If so, preventing CTRL + [S|L| and other known browser combinations would also make sense.
I think that's what the keyboard element hint is for (not taking over the entire window)... BTW, just noticed that the docs for that are a bit outdated (it takes a CSS selector now, '#canvas' is the only one that just happens to still work).
Hi everyone,
I'm compiling an MMORPG (client, of course) with Emscripten. I've already rewritten the window management (from platform-native <-to-> SDL2) and networking (from boost::asio <-to-> C sockets).
I've spent north of 60 hours porting so far, and honestly had a blast. Emscripten is amazing! Thank you all so much for all this great work.
Everything works great, except for keyboards events. The game needs to capture function keys (F1, F2, ..) and also a few key combinations (like CTRL + L). I've tried to
preventDefault
keyboard events on thedocument
and/orwindow
, but then the game doesn't receive those events as well.I've read a bit and found these two relevant options in SDL_hints.h:
I've set them up in the module's
preRun
:SDL_EMSCRIPTEN_KEYBOARD_ELEMENT
works well, and now the game only receives keyboard events if the canvas is in focus.However,
SDL_GRAB_KEYBOARD
doesn't prevent keyboard events from bubbling up to the window, so F5 still refresh.Here's the HTML & Module I'm using:
Can anybody help me figure out what else can I try here?