Skip to content

[Bug] Can't receive SDL_MOUSE_BUTTON_DOWN and SDL_EVENT_MOUSE_BUTTON_UP event #15369

@CoraBlack

Description

@CoraBlack

System info:

archLinux gnome wayland and windows 11 latest.

Issue

By my debug, I found the code can't always reach the case SDL_EVENT_MOUSE_BUTTON_DOWN and SDL_EVENT_MOUSE_BUTTON_UP

test code

#include <SDL3/SDL.h>

#include <cstdio>
#include <iostream>

// A window for event system to work 
struct Window {
    Window() {
        SDL_Init(SDL_INIT_VIDEO);

        win_ptr = SDL_CreateWindow("Window for input test", 640, 480, SDL_WINDOW_RESIZABLE);
        if (!win_ptr) {
            std::cerr << "Failed to create SDL window: " << SDL_GetError() << std::endl;
            std::exit(1);
        }

        renderer = SDL_CreateRenderer(win_ptr, nullptr);
        if (!renderer) {
            std::cerr << "Failed to create SDL renderer: " << SDL_GetError() << std::endl;
            std::exit(1);
        }

        // Provide first frame for window to be ready and input system to initialize properly
        // This is necessary for the test to work correctly in Linux wayland environment
        SDL_RenderClear(renderer);
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderPresent(renderer);

        SDL_ShowWindow(win_ptr);
    }

    ~Window() {
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(win_ptr);
    }

    SDL_Window* win_ptr = nullptr;
    SDL_Renderer* renderer = nullptr;
};

static const char* MouseButtonToString(Uint8 button) {
    switch (button) {
        case SDL_BUTTON_LEFT:
            return "Left";
        case SDL_BUTTON_MIDDLE:
            return "Middle";
        case SDL_BUTTON_RIGHT:
            return "Right";
        case SDL_BUTTON_X1:
            return "X1";
        case SDL_BUTTON_X2:
            return "X2";
        default:
            return "Unknown";
    }
}

int main(int, char**) {
    auto window = Window{};

    std::puts("Click mouse buttons inside the window. Press ESC or close window to quit.");
    std::puts("Valid button constants: LEFT=1, MIDDLE=2, RIGHT=3, X1=4, X2=5");

    bool running = true;
    while (running) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
                case SDL_EVENT_QUIT:
                    running = false;
                    break;

                case SDL_EVENT_KEY_DOWN:
                    if (event.key.key == SDLK_ESCAPE) {
                        running = false;
                    }
                    break;

                case SDL_EVENT_MOUSE_BUTTON_DOWN:
                case SDL_EVENT_MOUSE_BUTTON_UP: {
                    const SDL_MouseButtonEvent& b = event.button;
                    const char* action = b.down ? "DOWN" : "UP";
                    std::printf(
                        "Mouse %s | button=%u (%s) | clicks=%u | x=%.1f y=%.1f\n",
                        action,
                        static_cast<unsigned>(b.button),
                        MouseButtonToString(b.button),
                        static_cast<unsigned>(b.clicks),
                        b.x,
                        b.y
                    );
                    break;
                }

                default:
                    break;
            }
        }

        SDL_Delay(16);
    }

    SDL_Quit();
    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions