Stoatman
Stoatman

Reputation: 758

Event type set to SDL_MOUSEBUTTONDOWN even when not pressing any button

I am testing mouse events in SDL2. I want to print "button" to the console window whenever a mouse button is pressed (right, left and middle button). However, when I move my mouse without pressing any button, the message is also printed. Why is that?

while (!quit)
    {

        SDL_WaitEvent(&event);


        switch (event.type)
        {
        case SDL_QUIT:
            quit = true;
            break;

        default:

            if (event.type = SDL_MOUSEBUTTONDOWN)
            {
                std::cout << "button\n";
            }
            break;
    }

Upvotes: 0

Views: 193

Answers (1)

Alex
Alex

Reputation: 10126

You assign value instead of comparison. It must be:

 if (event.type == SDL_MOUSEBUTTONDOWN) // was '='

One of the ways to avoid such errors is using of Yoda notation:

if (SDL_MOUSEBUTTONDOWN == event.type)

Upvotes: 1

Related Questions