Hi. I'm trying to get DirectInput8 (on SDK version 9) to work. The thing is, everything initializes okay, all the function calls don't return errors. Everything should be fine.. but for some reason, it's not recognizing anything from my keyboard. Here is some code that demonstrates this:
This is how the input device is set:
if (FAILED (dinput->CreateDevice(deviceGUID, &_dinputdevice, NULL)))
return FALSE;
if (FAILED (_dinputdevice->SetDataFormat(_dataformat)))
return FALSE;
if (FAILED (_dinputdevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
return FALSE;
if (FAILED (_dinputdevice->Acquire()))
return FALSE;
return TRUE;
deviceGUID is passed as GUID_SysKeyboard, and _dataformat is of type LPCDIDATAFORMAT, and is set earlier to &c_dfDIKeyboard. No problems reported here, it returns just fine.
This code is how the device is read:
HRESULT hr;
while (true)
{
if (FAILED (_dinputdevice->Poll()))
return FALSE;
hr = _dinputdevice->GetDeviceState(_buffersize, &_buffer);
if (SUCCEEDED(hr))
{
return TRUE;
}
if ((hr == DIERR_INPUTLOST) || (hr==DIERR_NOTACQUIRED))
{
if (FAILED(_dinputdevice->Acquire()))
return FALSE;
}
else
return FALSE;
}
_buffersize is set to 256 earlier, and _buffer is declared as:
char _buffer[256];
Again, this function returns just fine (hr always succeeds).
This is my simple function that checks if a key was pressed:
BOOL eKeyboard::GetKeyState(unsigned char key)
{
return ((_buffer[key] & 0x80) ? TRUE : FALSE);
}
Of course, this is where the problem lies; the function always returns false. I did a check to see what the integer value of _buffer[key] is, and it's always zero. In the constructor of the class, I zeroed the memory of the buffer as so:
ZeroMemory (&_buffer, 256);
I think the problem is, the _buffer never gets updated correctly by GetDeviceState. I believe this is so because, originally, I did not have the ZeroMemory function. Without the ZeroMemory function, EVERY _buffer[whatever] value seemed to be non-zero and thus ALWAYS returned true (when checking the value, it was -51 or something). I'm not sure why this is not working.
Could it *possibly* be that in my main loop, the windows messaging system "interferes" with the input? I have this called in my main loop:
if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if(!GetMessage(&msg, NULL, 0, 0 )) return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Anyway, any help will be appreciated. Thanks!
[edited by - footlead on October 6, 2003 7:18:23 AM]
[edited by - footlead on October 6, 2003 7:19:15 AM]