Acquire or die!

Started by
8 comments, last by Samith 18 years, 9 months ago
Now, I've never messed with DirectX before, but I decided I have to since I don't want to use SDL anymore. All I'm using DirectX for is for it's DirectInput stuff, which I would very much like to make use of. However, I'm running into trouble. I've followed the tutorials from MSDN to get a keyboard device up and running, and everything works fine until I get to the part where I have to acquire the keyboard device. That always fails! Same with the mouse device! I don't know if source code will help, but here is what I believe to be relevant:

lpdi = 0;
	keyboard_device = 0;
	mouse_device = 0;

	HRESULT hr = DirectInput8Create(GetModuleHandle(0), 
									DIRECTINPUT_VERSION,
									IID_IDirectInput8,
									reinterpret_cast<LPVOID*>(&lpdi),
									0);
	if(FAILED(hr)) {
		MessageBox(0, "DirectInput8Create() failed miserably.", "IT CAN'T BE, BUT IT IS!", MB_OK);
		return false;
	}
	hr = lpdi->CreateDevice(GUID_SysKeyboard, &keyboard_device, 0);
	if(FAILED(hr)) { 
		MessageBox(0, "createdevice", ".", MB_OK);
		KillInput(); 
		return false; 
	}
	hr = keyboard_device->SetDataFormat(&c_dfDIKeyboard);
	if(FAILED(hr)) { 
		MessageBox(0, "setdataformat", ".", MB_OK);
		KillInput(); 
		return false; 
	}
	hr = keyboard_device->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	if(FAILED(hr)) { 
		MessageBox(0, "set co-op level failed", ".", MB_OK);
		KillInput(); 
		return false; 
	}
	if(keyboard_device) { keyboard_device->Acquire();
		if(FAILED(keyboard_device->Acquire())) {
			int x = GetLastError();
			MessageBox(0, "initial keyboard acquire", "!", MB_OK);
			KillInput();
			return false;
		}
	}

Advertisement
You're calling the Acquire function twice...

if(keyboard_device) { keyboard_device->Acquire();		if(FAILED(keyboard_device->Acquire())) {
The acquire still fails even when I take out the extra acquire. :(

EDIT: I notice I'm getting the error E_ACCESSDENIED when I try to acquire the keyboard.
What does the HRESULT say?
Quote:Original post by load_bitmap_file
What does the HRESULT say?

E_ACCESSDENIED :p I just editted my post up above before you posted.
It's very possible for the call to Acquire to fail the very first time or so. It's because the keyboard focus might still be on another window for a split second. Don't do any error checking on it, as it's not fatal. Just try to reacquire the keyboard in your input updating function. If you still can't acquire the keyboard after several frames, something might be wrong.
_______________________________________________________________________Hoo-rah.
Quote:Original post by Samith
Quote:Original post by load_bitmap_file
What does the HRESULT say?

E_ACCESSDENIED :p I just editted my post up above before you posted.


From the documentation:

DIERR_OTHERAPPHASPRIO
Another application has a higher priority level, preventing this call from succeeding. This value is equal to the E_ACCESSDENIED standard COM return value. This error can be returned when an application has only foreground access to a device but is attempting to acquire the device while in the background.

Perhaps your window isn't in focus? Verify that the window is in focus before attempting to acquire or set it to background access.
Ahh that made the acquire work. Thanks! The rest still isn't working perfectly, such as, when I acquire my mouse it basically just...makes there be no mouse at all which is somewhat dismaying, that and checking for keyboard input doesn't work, but I should be able to sort this out on my own.
Quote:Original post by Samith
Ahh that made the acquire work. Thanks! The rest still isn't working perfectly, such as, when I acquire my mouse it basically just...makes there be no mouse at all which is somewhat dismaying, that and checking for keyboard input doesn't work, but I should be able to sort this out on my own.


Assuming Drakex's method worked, and that you changed the cooperative level to background, set it back to foreground access. Background access is meant for applications that kind of sit out of the way, which doesn't really fit the descriptor for games.
Quote:Original post by load_bitmap_file
Quote:Original post by Samith
Ahh that made the acquire work. Thanks! The rest still isn't working perfectly, such as, when I acquire my mouse it basically just...makes there be no mouse at all which is somewhat dismaying, that and checking for keyboard input doesn't work, but I should be able to sort this out on my own.


Assuming Drakex's method worked, and that you changed the cooperative level to background, set it back to foreground access. Background access is meant for applications that kind of sit out of the way, which doesn't really fit the descriptor for games.

No, I left it at foreground access, I just changed the time when I initialized all the input stuff to after I had called SetFocus. Then I made some modifications to make sure my program doesn't call any input functions while the window isn't active to avoid breaking my keyboard device.

This topic is closed to new replies.

Advertisement