Sign in to follow this  
COleException

DInput: Mousebutton always pressed

Recommended Posts

Hi, I am having a bit trouble with DirectInput, "rgbButtons[0]" always returns the Button pressed state "0x80", even if my left mouse button ist NOT pressed. Can someone give me a hint, what's wrong in my code? Here the Init-function:
	if (FAILED(DirectInput8Create(
						hInst,
						DIRECTINPUT_VERSION,
						IID_IDirectInput8,
						(void**)&m_pDirinp,
						NULL)))
	{
		return FALSE;
	}
	
	if (FAILED(m_pDirinp->CreateDevice(GUID_SysMouse, &m_pDirinpDevice,NULL)))
	{
		return FALSE;
	}
	
	if (FAILED(m_pDirinpDevice->SetDataFormat(&c_dfDIMouse)))
	{
		return FALSE;
	}


	if (FAILED(m_pDirinpDevice->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
	{
		return FALSE;
	}


	if (FAILED(m_pDirinpDevice->Acquire()))
	{
		MessageBox(0, "m_pDirinpDevice->Acquire()", "ERROR!", MB_OK);
		return FALSE;
	}


Here the function that ALWAYS sets bMouseButtonPressed = true:
int CInput::GetMousePos(bool &bMouseButtonPressed)
{
	int iRet = 0;
	HRESULT hres = 0;
	hres = m_pDirinpDevice->Poll();

	if (hres == DIERR_INPUTLOST)
		m_pDirinpDevice->Acquire();

	DIMOUSESTATE2 MouseState;
	m_pDirinpDevice->GetDeviceState(sizeof(DIMOUSESTATE2), &MouseState);

	long xPos = MouseState.lX;
	long yPos = MouseState.lY;

	// Button pressed?
	if (MouseState.rgbButtons[0] & 0x80)
	{
		bMouseButtonPressed = true; // alway true. Why ?!
	}
	else
	{
		bMouseButtonPressed = false;
	}
-Mouseposition-code snipped-

Thanks in advance!

Share this post


Link to post
Share on other sites
From the code you've posted the only thing that strikes me as inconsistent is your use of SetDataFormat(&c_dfDIMouse) which matches with DIMOUSESTATE rather than DIMOUSESTATE2 which you're using. Could be worth trying to get those matched up correctly.

Have you tried stepping though the code with the debugger? Outputting the value of rgbButtons[]?

Seems that you're using polling rather than buffered, but I remember getting caught out a few times with buffered where it'd send a message with a '0' value that tripped up the logic I had written... maybe it is actually working but your logic test isn't working how you think it is/should?

Finally, cross-reference against the keyboard sample code (Samples\C++\DirectInput\Keyboard\) as it should implement this stuff correctly [smile]

hth
Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
From the code you've posted the only thing that strikes me as inconsistent is your use of SetDataFormat(&c_dfDIMouse) which matches with DIMOUSESTATE rather than DIMOUSESTATE2 which you're using. Could be worth trying to get those matched up correctly.


Using DIMOUSESTATE rather than DIMOUSESTATE2 fixed my problem immediately, thank you very much! :-)


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