Sign in to follow this  
Vieo

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

Recommended Posts

Vieo    122
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.)

Share this post


Link to post
Share on other sites
Koshmaar    989
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).

Share this post


Link to post
Share on other sites
Mushu    1396
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 == down

on 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).

Share this post


Link to post
Share on other sites
silverphyre673    454
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 y
void GetMouseCoordinate(int & x, int & y)
{
SDL_GetMouseState(&x, &y);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this