Search code examples
sdljbuttonjoystick

SDL2 jbutton enum?


I'm having some trouble figuring out what enums I can/should use for SDL2's gamepad/joystick support. I tried using "SDL_CONTROLLER_*", but I ended up with some odd results. For instance:

  SDL_Event e;
    while (SDL_PollEvent(&e) != 0)
    {
       if (e.type == SDL_JOYBUTTONDOWN)
       {
            printf("%d\n", e.jbutton.button);
            if (e.jbutton.button == SDL_CONTROLLER_BUTTON_B)
            {
                printf("HI\n");
            }
        }
    }

I had this code in my function's main loop, and the second print statement goes off when I press the D-pad's DOWN button, instead of when I press the B button. I'm using an XBOX 360 controller, and it gives me no trouble at all when I play other games. Am I even using the correct enum?


Solution

  • A controller has different input handling than the Joystick.

    You need to use Controller events:

    SDL_CONTROLLERAXISMOTION: controller axis motion
    SDL_CONTROLLERBUTTONDOWN: controller button pressed
    SDL_CONTROLLERBUTTONUP: controller button released
    SDL_CONTROLLERDEVICEADDED: controller connected
    SDL_CONTROLLERDEVICEREMOVED: controller disconnected
    SDL_CONTROLLERDEVICEREMAPPED: controller mapping updated
    

    As you can see in this page https://wiki.libsdl.org/SDL_EventType.