# DInput: Mousebutton always pressed

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

## 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-



##### 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 on other sites
Quote:
 Original post by jollyjeffersFrom 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! :-)