Code:
#include "CDInput.h"
//Helpers
#define DIKEYDOWN(data,n) (data[n] & 0x80)
class CKeyboard
{
protected:
IDirectInput8 *_pDI8;
IDirectInputDevice8 *_pDID8;
public:
//So anyone can access our data - otherwise function overhead per use
//could inline....but
UCHAR keystate[256];
CKeyboard(IDirectInput8 *pDI8) {_pDI8=pDI8;};
~CKeyboard() {ShutDown();};
void ShutDown(void) {//if (_pDID8) _pDID8->Unacquire();
if (_pDID8) _pDID8->Release();};
HRESULT Init(HWND hwnd);
HRESULT Update(void);
inline bool KeyDown(UCHAR key)
{
if (keystate[key])
{
return true;
} else return false;
}
inline bool KeyUp(UCHAR key)
{
if (keystate[key])
{
return false;
} else return true;
}
};
Code:
#include "CKeyboard.h"
HRESULT CKeyboard::Init(HWND window)
{
//Create device
if (FAILED(_pDI8->CreateDevice(GUID_SysKeyboard,
&_pDID8,
NULL)))
{
::MessageBox(0,"Could not create keyboard device",0,0);
}
//Set cooperative level
if (FAILED(_pDID8->SetCooperativeLevel(window,
DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
{
::MessageBox(0,"Could not set keyboard cooperative level",0,0);
}
//Set data format
if (FAILED(_pDID8->SetDataFormat(&c_dfDIKeyboard)))
{
::MessageBox(0,"Could not set keyboard data format",0,0);
}
//Acquire the keyboard
if (FAILED(_pDID8->Acquire()))
{
::MessageBox(0,"Could not acquire keyboard device",0,0);
}
return true;
}
HRESULT CKeyboard::Update(void)
{
HRESULT hr;
//Make sure we still have the device
//If so fill keystate with current state values representing current
//keyboard state
hr=(_pDID8->GetDeviceState(sizeof(keystate),(LPVOID)&keystate));
if (hr==DIERR_INPUTLOST)
{
::MessageBox(0,"Input lost",0,0);
}
return true;
}
This works fine for me. This is code from my input system which I've used in several projects - and a friend of mine has used it with OpenGL and we have no issues with it. Not sure what is going on with yours. Look at my code and perhaps you might be able to compare it with yours to see what is different or what is causing the error.
If you'll notice I commented out the unacquire() code because I received the same error. It seems that you do not need to unacquire the device before releasing it.