This seems to work
void Win32Keyboard::_readBuffered()
{
DIDEVICEOBJECTDATA diBuff[KEYBOARD_DX_BUFFERSIZE];
DWORD entries = KEYBOARD_DX_BUFFERSIZE;
HRESULT hr;
// KevinJ - Set to true after we alt-tab
static bool verifyAfterAltTab=false;
hr = mKeyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
if( hr != DI_OK )
{
hr = mKeyboard->Acquire();
// KevinJ - E_ACCESSDENIED is returned when I alt-tab. Maybe other stuff is returned too?
if (hr==E_ACCESSDENIED)
verifyAfterAltTab=true;
while( hr == DIERR_INPUTLOST )
hr = mKeyboard->Acquire();
return;
}
if( FAILED(hr) )
OIS_EXCEPT( E_General, "Win32Keyboard::_readBuffered() >> Problem with Device!" );
//Update keyboard and modifier states.. And, if listener, fire events
for(unsigned int i = 0; i < entries; ++i )
{
//If the listener returns false, that means that we are probably deleted...
//send no more events and just leave as the this pointer is invalid now...
bool ret = true;
KeyCode kc = (KeyCode)diBuff[ i ].dwOfs;
//Store result in our keyBuffer too
KeyBuffer[kc] = static_cast<unsigned char>(diBuff[ i ].dwData);
if( diBuff[ i ].dwData & 0x80 )
{
//Turn on modifier
if( kc == KC_LCONTROL || kc == KC_RCONTROL )
mModifiers |= Ctrl;
else if( kc == KC_LSHIFT || kc == KC_RSHIFT )
mModifiers |= Shift;
else if( kc == KC_LMENU || kc == KC_RMENU )
mModifiers |= Alt;
if( listener )
ret = listener->keyPressed( KeyEvent( this, diBuff[i].dwTimeStamp, kc, _translateText(kc) ) );
}
else
{
//Turn off modifier
if( kc == KC_LCONTROL || kc == KC_RCONTROL )
mModifiers &= ~Ctrl;
else if( kc == KC_LSHIFT || kc == KC_RSHIFT )
mModifiers &= ~Shift;
else if( kc == KC_LMENU || kc == KC_RMENU )
mModifiers &= ~Alt;
//Fire off event
if( listener )
ret = listener->keyReleased( KeyEvent( this, diBuff[i].dwTimeStamp, kc, 0 ) );
}
if(ret == false)
break;
}
// KevinJ - Check the buffer against the current keystate, and send events where it is wrong
if (verifyAfterAltTab==true)
{
bool ret = true;
unsigned char keyBufferCopy[256];
memcpy(keyBufferCopy, KeyBuffer, 256);
_read();
unsigned i;
for (i=0; i < 256; i++)
{
if (keyBufferCopy[i]!=KeyBuffer[i])
{
if (listener)
{
if (KeyBuffer[i])
ret = listener->keyPressed( KeyEvent( this, 0, (KeyCode)i, _translateText((KeyCode)i) ) );
else
ret = listener->keyReleased( KeyEvent( this, 0, (KeyCode)i, 0 ) );
}
}
if(ret == false)
break;
}
verifyAfterAltTab=false;
}
}