2

I'm having a curious little problem polling mouse events using SDL.

        if(event.type == SDL_MOUSEBUTTONDOWN) {
            printf("click %d\n", event.button.button);
        }
        else if(event.type == SDL_MOUSEMOTION) {
            printf("move %d\n", event.button.button);


        }

If I click and hold the right mouse button while moving the mouse I get:

click 3 // = SDL_BUTTON_RIGHT
move 4  // != SDL_BUTTON_RIGHT !
move 4
...

i.e. SDL_MOUSEBUTTONDOWN is setting button.button to 3 for right mouse button while SDL_MOUSEMOTION is setting it to 4.

Does anyone know why this might be happening?

user1483596
  • 309
  • 2
  • 12

2 Answers2

1

You're supposed to use event.motion for SDL_MOUSEMOTION, not event.button. Furthermore, event.motion.state is a bitmask, you're supposed to query it with event.motion.state & SDL_BUTTON(3).

avakar
  • 32,009
  • 9
  • 68
  • 103
  • This does work - though it seems from a brief experiment that `event.button = event.motion.state` at all times. Annoyingly on a right click `event.button.button := SDL_BUTTON_RIGHT (= 3)` and on a right click motion, `event.button.button := SDL_BUTTON(SDL_BUTTON_RIGHT) (= 4...)` so it's not as clean as I'd have liked. (A bit annoying that it's not always one or the other... – user1483596 Oct 17 '12 at 19:47
  • Thats because, with motion, you can have multiple mouse buttons (it's a state of or-masks). With a button down or up event, there's no way to have multiple buttons, so it just returns the 'index' of the button rather than the mask. I don't know about then, but now masks have their own constants. `SDL_BUTTON_RMASK = SDL_BUTTON(SDL_BUTTON_RIGHT)` – Mark Jeronimus Aug 02 '19 at 16:53
0

event.button.button is used for the click event not for motion, I don't understand the purpose of this code?

Solidus
  • 251
  • 2
  • 10
  • I'm just checking which (if any) button is pressed during the motion. I'd have thought event.button.button would keep this information (it works fine for left mouse button - event.button.button is 1 in each if clause). – user1483596 Oct 17 '12 at 19:27