The mouse button state AND bit masking returns true for multiple Mouse buttons

Hello,

I am currently learning SDL2.
I recently tried to capture mouse event Button Up, and want to do logics when Left Button is released.

  while (game_loop) {
    if (SDL_WaitEvent(&e)) {
      switch (e.type) {
        case SDL_MOUSEBUTTONUP:
          if ( e.motion.state & SDL_BUTTON_LMASK ) {
            // logics when left button is released
          }

But the behavior is not what I expected. I expect the if-condition is reached only when Left Button of my mouse is relesead. But, it satisfies the SDL_BUTTON_LMASK bit masking if-condition when Left, Right, and Forward page button is released.

I also tried with this if-test

          if ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON_LMASK ) {

and with this, I can never satisfies the if-condition.

What Could I do wrong in my code?
Thanks

My bad. I am testing the wrong event. I tested motion event instead of button event.

With this line, the behavior is as I expected

          if ( e.button.button == SDL_BUTTON_LEFT ) {

3 Likes