Home > Enterprise >  SDL remembers last instance"s user input
SDL remembers last instance"s user input

Time:08-14

I am working on an SDL project in C and I am having trouble with the input handling.

whenever I start my SDL app it remembers the last app's inputted keys and SDL_PollEvent always gives repeatedly the last key that was pressed in the previous instance of the program.

btw I am compiling the code in a WSL window I have tried the same piece of code in a visual studio environment and it worked perfectly fine. So can someone explain what's happening?

Here is the sample code:

#include <SDL2/SDL.h>
#include <stdio.h>

int main() {
    SDL_Event event;

    SDL_Window* sdlwind;
    if(SDL_Init(SDL_INIT_VIDEO) != 0) {
        fprintf(stderr, "SDL_Init Error : %s", SDL_GetError());
        return 1;
    }
    sdlwind = SDL_CreateWindow("Engine", 0, 0, 1080, 840, SDL_WINDOW_SHOWN);

    if(!sdlwind) {
        fprintf(stderr, "SDL_CreateWindow Error : %s", SDL_GetError());
        return 1;
    }

    printf("Start");
    while (1)
    {
        while (SDL_PollEvent(&event))
        {
            printf("%c\n", event.key.keysym.sym);
        }

        if(event.key.keysym.sym == SDLK_a) {
            break;
        }
    }
    
    SDL_DestroyWindow(sdlwind);
    SDL_Quit();
    
    return 0;
}

This sample code has outputted the first time:

OUTPUT
,
,
,
,
,
,
// etc since the last key pressed in the previous instance was , and after terminating the program by pressing a
// the next output becomes
OUTPUT
a
a
a
a
a
// etc

I have tried removing the executable and launching another SDL program but the same problem persists. I have later tried to take input normally with getchar() but it didn't register any key so I am not sure but it might not be a problem related to the stdin it must be smthng with SDL and WSL.

CodePudding user response:

It sounds like you are experiencing the same root problem as in this Super User question and issue #58 against WSLg.

It's fixed in the latest Preview releases, which you can install from this Microsoft Store link (since you seem to be on Windows 11).

The Github issue was never closed, and I never saw any release notes on it, so it may have been an upstream fix (perhaps FreeRDP), but it definitely is resolved for both me as well as the OP of that Super User question.

  • Related