Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Real World

Mouse clicking

This topic is 5935 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''m using Direct Input 8 to check the mouse for left clicks in my game for the purpose of navigating through a selection of menu screens. The problem I''m having is that when one button is clicked and the screen loads the next option menu, the button below that gets clicked too as the input is checked again and the mouse is still registered as being pressed. Is there a good way to reset the state of the mouse after each check so this doesnt happen? Cheers Dave
		// Make sure the mouse has been initialized
		if( !m_bInitialized )
			return FALSE;

		// Get the state of the mouse into the key buffer
		r = m_pMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &m_dims2 );
		if( FAILED( r ) )
		{
			// If the device is not acquired...
			if( r == DIERR_INPUTLOST  || r == DIERR_NOTACQUIRED)
			{
				// ...then reacquire the device
				while( r == DIERR_INPUTLOST  || r == DIERR_NOTACQUIRED)
					r = m_pMouse->Acquire();

				if( SUCCEEDED( r ) )
					m_pMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &m_dims2 );
				else
					return FALSE;
			}
			else
				// ...Otherwise it was some other error
				return FALSE;
		}

		// Check if the mouse was clicked
		if( key & 0x80 )
			return TRUE;
		else
			return FALSE;
 

Share this post


Link to post
Share on other sites
Advertisement
What I do is I store whether or not the mouse button was pressed down last frame. So to check for a click, I check to see if it is currently pressed, but wasn''t pressed last frame.

tj963

Share this post


Link to post
Share on other sites
I''m pretty sure I have that.
The "hack" i have at the moment is to set a trigger when a mouse is clicked. It then will not accept mouse clicks for 6 or seven frames but that doesnt appear to work!

Share this post


Link to post
Share on other sites
Instead of just checking if the mouse is down, have a second variable that is for a "click". It gets set to true if the mouse is down, gets set to false when it gets accessed (when you check it, use a function), and can''t be set again until the mouse has been released (needing a thrid variable lock). That way you only get one "event" per mouse click. If the user double-clicks on a button then its their own fault.

Karg

Share this post


Link to post
Share on other sites


  
// Make MouseDown either global, or static within the function

bool MouseDown = false;


// Check if the mouse was clicked


bool MouseClickedThisFrame = false;
if (key & 0x80)
{
if (MouseDown == false)
{
MouseClickedThisFrame = true;
}

MouseDown = true;
}
else
{
MouseDown = false;
}

return MouseClickedThisFrame;



Alan

Share this post


Link to post
Share on other sites
I have done the following.


First add the following to an appropriate header file

#define MOUSE_LEFT (0) // Standard mouse buttons
#define MOUSE_MIDDLE (2)
#define MOUSE_RIGHT (1)
#define MOUSE_LLEFT (3) // Left side on 5 button mouse
#define MOUSE_RRIGHT (4) // Right Side on 5 button mouse

#define MOUSEDOWN(name,button)(name.rgbButtons[button] & 0x80)
#define MOUSE_DOWN(button) (MOUSEDOWN(m_dims2,button))
#define MOUSE_PRESS(button) ((MOUSEDOWN(m_dims2,button) && !MOUSEDOWN(old_m_dims2,button)))
#define MOUSE_RELEASE(button) ((!MOUSEDOWN(m_dims2,button) && MOUSEDOWN(old_m_dims2,button)))

DIMOUSESTATE2 old_m_dims2


Then prior to getting the current mouse state I have

memcpy(&old_m_dims2, &m_dims2, sizeof(m_dims2));

MOUSEDOWN would not normally be used, however;

MOUSE_DOWN returns true if the mouse button is down
MOUSE_PRESS returns true if the mouse was pressed since was last checked
MOUSE_RELEASE returns true if the mouse was released since last checked

Thus your program would just use

if MOUSE_PRESS(MOUSE_LEFT) { ''do whatever}

Stephen

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!