SDL: The proper way to determine if a mousebutton is held down?

Started by
2 comments, last by silverphyre673 18 years, 6 months ago
I've been trying to get an understanding of mouse input and how events are handled. I've been able to figure out how to get a program to determine which button was pressed, but for the life of me, I haven't been able to get a program to recognize the fact that a button is being held down. Here is my eventhandler(please excuse the mess =P). Just pretend there is a small blank window that pops up when the program starts. ================================================================== while(isRunning) { SDL_PollEvent(&event); if(event.type == SDL_QUIT) isRunning = false; if(event.button.button == SDL_BUTTON_LMASK){ while (event.button.state == SDL_PRESSED){ SDL_PollEvent(&event); SDL_WM_SetCaption("Held down.",0); }//end while loop }//end if SDL_WM_SetCaption("Not held down.",0); }//end eventhandler ================================================================= Basically, I want the title bar to display "held down" for as long as I hold down on the left mousebutton. It works somewhat in the sense that when I'm not pressing down, it lets me know. The problem is when I click and hold, it will display "held down" on the titlebar, but not infinitely before I let go. If I set the mouse in motion without releasing, the message will change. Also, even if I don't release the button the message will change to "Not held down" eventually. What am I doing wrong? =) Also, can someone point me to a really good mouse tutorial that gives examples of common things you eventually want to do with a mouse? (e.g. holding down a specific mousebutton and have an action performed until you release it.)
Advertisement
Have you read SDL Bible? [wink] I'm pretty sure you'll find there answers to all of your questions. Just read API section about event handling, then FAQ and Examples.

As for this problem, I suspect that SDL_PRESSED is triggered only once, when you click mouse button for the first time - in next frame it's cleared by SDL even if you're still pressing that button (but don't take my bet on it).

Yep, you'll only get one event when the button is pressed (so it'll last one frame)

What you need to do is buffer the current buffer state:
bool buttonState; // true == downon ButtonPress { buttonState = true; }on ButtonRelease { buttonState = false; }

That way buttonState will always have the correct mouse state. You can differ the two by the type in SDL_MouseButtonEvent - SDL_MOUSEBUTTONDOWN or SDL_MOUSEBUTTONUP.

Alternativly, you could change the caption to "Button Pressed" when you first get the SDL_MOUSEBUTTONDOWN event, and only change it back when you get the SDL_MOUSEBUTTONUP event (but this seems like a less versitile method).
This works:

bool LeftMouseDown(){    return SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(0);}bool MiddleMouseDown(){    return SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1);}bool RightMouseDown(){    return SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(2);}//puts the coordinate of the mouse cursor into x and yvoid GetMouseCoordinate(int & x, int & y){    SDL_GetMouseState(&x, &y);}
my siteGenius is 1% inspiration and 99% perspiration

This topic is closed to new replies.

Advertisement