Jump to content
  • Advertisement
Sign in to follow this  
Vieo

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

This topic is 4695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!