Archived

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

Acquiring keyboard + mouse

This topic is 5242 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

Hi, im having a huge problem with DirectInput, the books im reading and the MS source code examples are telling me to set the cooperative level of the keyboard and mouse to r = m_pKeyboard->SetCooperativeLevel(m_hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); r = m_pMouse->SetCooperativeLevel(m_hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND); They both set the cooperative level ok, but when i goto acquire the keyboard and mouse, both of these results in: r = E_ACCESSDENIED The only way for me to get it to work properly is if i change the flags to: r = m_pKeyboard->SetCooperativeLevel(m_hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE); r = m_pMouse->SetCooperativeLevel(m_hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); Obviously i would prefer to use exclusive for the mouse as it hides the mouse pointer for you etc. When i run the MS code examples they are fine, and i cant see much difference in the code. Here is what im using: Keyboard --------
	// Create the device for the keyboard

	r = m_pDInput->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL);
	if (FAILED(r))
	{
		ReportError("DX Input", "Failed to create keyboard device");
		return E_FAIL;
	}

	// Set the data format for the device

	r = m_pKeyboard->SetDataFormat(&c_dfDIKeyboard);
	if (FAILED(r))
	{
		ReportError("DX Input", "Unable to set the keyboard data format");
		return E_FAIL;
	}

	// Set the cooperative level

	//r = m_pKeyboard->SetCooperativeLevel(m_hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);

	r = m_pKeyboard->SetCooperativeLevel(m_hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);

	if (FAILED(r))
	{
		ReportError("DX Input", "Unable to set the keyboard cooperative level");
		return E_FAIL;
	}

	// Acquire the device

	r = m_pKeyboard->Acquire();
	if (FAILED(r))
	{
		ReportError("DX Input", "Unable to acquire the keyboard");
		return E_FAIL;
	}
Mouse -----
	r = m_pDInput->CreateDevice(GUID_SysMouse, &m_pMouse, NULL);
	if (FAILED(r))
	{
		ReportError("DX Input", "Unable to create mouse device");
		return E_FAIL;
	}

	// Set the data format for the mouse

	r = m_pMouse->SetDataFormat(&c_dfDIMouse2);
	if (FAILED(r))
	{
		ReportError("DX Input", "Unable to set the mouse data format");
		return E_FAIL;
	}

	// Set the cooperative level for the mouse

	r = m_pMouse->SetCooperativeLevel(m_hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
	if (FAILED(r))
	{
		ReportError("DX Input", "Unable to set the cooperative level for the mouse");
		return E_FAIL;
	}

	// Acquire the physical mouse into the device

	r = m_pMouse->Acquire();
	if (FAILED(r))
	{
		ReportError("DX Input", "Unable to acquire mouse");
		return E_FAIL;
	}
Any help you can shed on this would be much appreciated as it is driving me insane!! Thanks

Share this post


Link to post
Share on other sites
The E_ACCESSDENIED error is the standard COM return that in DI also means DIERR_OTHERAPPHASPRIO.
So, another application has priority - perhaps you''re running from in the debugger and VC++ (or whatever you''re using) is in the foreground while your app doesn''t have focus?

-Mezz

Share this post


Link to post
Share on other sites
Lo Mezz,

after much reading on the internet ive found that the problem was me calling my DXInput->Init() function before the window had actually been shown.

it worked fine once i made sure the window got displayed first.

Thanks for reply.

on a side note, you know the DIMOUSESTATE and DIMOUSESTATE2 structures? when i click my 5th mouse button with DIMS2 it thinks it is constantly pressed. anyone had any experience with DIMS2?

cheers

Share this post


Link to post
Share on other sites
quote:
on a side note, you know the DIMOUSESTATE and DIMOUSESTATE2 structures? when i click my 5th mouse button with DIMS2 it thinks it is constantly pressed. anyone had any experience with DIMS2?


Only difference between these two structures is that the second one can handle mouse with more buttons (up to 8).
How you check if the button is pressed or not ?

SirMike

Share this post


Link to post
Share on other sites
The button checking works fine upto the 4th mouse button, it just goes crazy when it hits five.

i use:

// Return the button status from the buffer
if (m_MouseData.rgbButtons[Button] & 0x80)
return true;
else
return false;

i pass through 0 - 3 for buttons 1-4, but then when i press the 5th mouse button it constantly triggers thinking its pressed down

Share this post


Link to post
Share on other sites
Are you sure the 5th button really is a mouse button? On some mice some buttons are treated as keys. Btw, do you set the entire struct to 0?

[edited by - Enselic on August 7, 2003 10:52:02 AM]

Share this post


Link to post
Share on other sites
err i dont know what you mean about it really being a mouse button.

it is a MS intellimouse explorer

i have just checked and i dont think i initialise the whole thing to 0 anywhere...but surely that shouldnt matter after the first time the mouse has been polled?

would i clear it like this?

ZeroMemory(&m_MouseData, sizeof(DIMOUSESTATE2));

thanks for help

Share this post


Link to post
Share on other sites
Yeah, you zero it right.

What I meant was that some mice you can have a button to act like TAB as an example. So when you press the 5th button it could be like pressing any other keyboard-key.

Share this post


Link to post
Share on other sites
oh yeah, my middle mouse button is set as double click, so its like sending 2 mouse button 1 clicks.

but mouse button 4 + 5 are untouched, they work fine in the MS examples, so i must be doing something wrong...

Share this post


Link to post
Share on other sites