Emscripten: up and down keys scroll browser

In the latest version of Emscripten (3.1.30, which comes with SDL 2.24.2) pressing the up and down arrow keys causes the browser to scroll, as well as generating SDL_KEYDOWN events. This is generally undesirable and used not to happen.

Is there a way of disabling this behavior so the keys only get sent to SDL2? I don’t mind if the PgUp and PgDn keys scroll the browser (although it would be nice to be able to disable that too).

Even if you don’t know the solution, can somebody at least tell me whether it is likely to be an SDL2 issue or an Emscripten issue, so I know the best place to pursue it?

This turned out to be because I was calling SDL_SetEventFilter() in my code. Changing this to SDL_AddEventWatch() restored the desired behavior of the arrow keys not scrolling the browser (canvas). I don’t claim to understand why calling SDL_SetEventFilter() had that effect (especially since it didn’t in Emscripten 2.0.5) but I’m happy to have a solution.

I think there’s an use case, say you want to have a text editor to write basic code and then build and run in your engine, all in the same html page, so you may want to be able to either capture or not those keys.

I am curious if it’s handling focus or if it can be somehow tied to focus - say if the canvas has focus it captures the keys and of not, then it doesn’t.

But it’s good you found a fix for your case.

If you want all the keys to go to the canvas rather than to SDL2 you can do:


But that doesn’t help if you want to be selective. The list of keys which are treated as ‘navigation’ keys and are filtered from going to the canvas is in SDL_emscriptenevents.c:

    if ( (scancode == SDL_SCANCODE_BACKSPACE) ||
         (scancode == SDL_SCANCODE_TAB) ||
         (scancode == SDL_SCANCODE_LEFT) ||
         (scancode == SDL_SCANCODE_UP) ||
         (scancode == SDL_SCANCODE_RIGHT) ||
         (scancode == SDL_SCANCODE_DOWN) ||
         ((scancode >= SDL_SCANCODE_F1) && (scancode <= SDL_SCANCODE_F15)) ||
         keyEvent->ctrlKey ) {
        is_nav_key = SDL_TRUE;

Short of editing this file and compiling the Emscripten SDL port from source (which is very inconvenient compared with simply using -s USE_SDL=2) the list is fixed and not customisable. I have suggested that being able to do so would be a worthwhile improvement.