Home > Blockchain >  How to detect if left mousebutton is being held down with SDL2
How to detect if left mousebutton is being held down with SDL2

Time:02-08

I know how to use events to detect when the mouse button is pressed, however, how do detect if a mouse button is currently held down? Note I am not asking how to get it from an event.

I have not tried anything apart from SDL_GetMouseState(&x, &y); however it seems to only return the X and Y position of the mouse.

CodePudding user response:

From my understanding a state machine should do the trick. If you detect a MB Down event (SDL_PRESSED), set a variable to hold that information for you, if you detect a MB Up event (SDL_RELEASED), reset the variable. So reading the content of this variable would return the "held down" information.

Side info: Depending on the mouse you are using the button state may bounce between high and low state. If that is the case, add a samll (~1ms) delay.

Because i do not have SDL2 currently on my machine, i can not provide code. Sorry for that.

In case you have trouble finding the SDL2 docu for this because they reworked their wiki, here is a link: https://wiki.libsdl.org/SDL_MouseButtonEvent

But let me know if this was helpful.

  •  Tags:  
  • Related