#include "StdAfx.h" #include "keyboardagent.h" #include "ShortcutManager.h" cKeyboardAgent::cKeyboardAgent() : mpDidKey(NULL) , mBufferedKeys(0) , mPrevBuffer(0) { } cKeyboardAgent::~cKeyboardAgent() { Release(); } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : Create( HWND hWnd, LPDIRECTINPUT8 pdi ) // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ HRESULT cKeyboardAgent::Create( HWND hWnd, LPDIRECTINPUT8 pDi ) { HRESULT hr; unsigned long coopFlags = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND | DISCL_NOWINKEY; hr = pDi->CreateDevice( GUID_SysKeyboard, &mpDidKey, NULL ); hr |= mpDidKey->SetDataFormat( &c_dfDIKeyboard ); hr |= mpDidKey->SetCooperativeLevel( hWnd, coopFlags ); DIPROPDWORD dipdw; dipdw.diph.dwSize = sizeof(DIPROPDWORD); dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); dipdw.diph.dwObj = 0; dipdw.diph.dwHow = DIPH_DEVICE; dipdw.dwData = KEYBOARD_BUFFER; hr = mpDidKey->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph ); mpDidKey->Acquire(); ClearKeyState(); return hr; } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : Release() // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ void cKeyboardAgent::Release() { if( mpDidKey ) mpDidKey->Unacquire(); SAFE_RELEASE( mpDidKey ); } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : UpdateKeyboardState() // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ void cKeyboardAgent::UpdateKeyboardState() { HRESULT hr = E_FAIL; mBufferedKeys = KEYBOARD_BUFFER; hr = mpDidKey->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), mDidodKey, &mBufferedKeys, 0 ); if( FAILED( hr ) ) { do { hr = mpDidKey->Acquire(); } while( hr == DIERR_INPUTLOST ); ClearKeyState(); //hr = mpDidKey->Acquire(); //while( hr == DIERR_INPUTLOST ) // hr = mpDidKey->Acquire(); //if(SUCCEEDED(hr)) //{ // ClearKeyState(); // return; //} } } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : GetNextKeyState() // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ bool cKeyboardAgent::GetNextKeyState() { if(mBufferedKeys > KEYBOARD_BUFFER) return false; mKeyEvent = false; DWORD keyindex; unsigned long i; // Űº¸µå »óÅ ó¸® for(i=0; imKey; unsigned long mixKey = pKeyInfo->mMixKey; if( key < 256 && mixKey < 256 ) { if( mixKey == 0 ) { if( mKeyState[key] == _KEY_DOWN ) return true; } else { if( mKeyState[mixKey] == _KEY_PRESS && mKeyState[key] == _KEY_DOWN ) return true; } } return false; } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : GetKeyUp( unsigned long key ) // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ bool cKeyboardAgent::GetKeyUp( unsigned long key ) { if( key < 256) return (mKeyState[key] == _KEY_UP); else return false; } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : GetKeyUp( sKeyboardInfo* pKeyInfo ) // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ bool cKeyboardAgent::GetKeyUp( sKeyboardInfo* pKeyInfo ) { if( pKeyInfo == NULL ) return false; unsigned long key = pKeyInfo->mKey; unsigned long mixKey = pKeyInfo->mMixKey; if( key < 256 && mixKey < 256 ) { if( mixKey == 0 && mKeyState[key] == _KEY_UP ) return true; } return false; } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : GetKeyPress( unsigned long key ) // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ bool cKeyboardAgent::GetKeyPress( unsigned long key ) { if( key < 256) return (mKeyState[key] == _KEY_PRESS); else return false; } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : GetKeyPressed( unsigned long key ) // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ bool cKeyboardAgent::GetKeyPressed( unsigned long key ) { if( key < 256) return (mKeyState[key] > _KEY_UP); else return false; } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : GetKeyPressed( sKeyboardInfo* pKeyInfo ) // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ bool cKeyboardAgent::GetKeyPressed( sKeyboardInfo* pKeyInfo ) { if( pKeyInfo == NULL ) return false; unsigned long key = pKeyInfo->mKey; unsigned long mixKey = pKeyInfo->mMixKey; if( key < 256 && mixKey < 256 ) { if( mixKey == 0 && mKeyState[key] > _KEY_UP ) return true; } return false; } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : GetKeyState( unsigned long key ) // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ int cKeyboardAgent::GetKeyState( unsigned long key ) { if( key < 256) return mKeyState[key]; else return _KEY_NONE; } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : ClearKeyState() // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ void cKeyboardAgent::ClearKeyState() { mBufferedKeys = 0; mPrevBuffer = 0; memset( mBufferKey, 0, sizeof(unsigned char)*KEYBOARD_BUFFER ); memset( mKeyState, 0, sizeof(unsigned char)*256 ); } // * ------------------------------------------------------------------ // * ÇÔ¼öÀ̸§ : IsEmptyKeyboardEvent() // * ¸ñ Àû : // * ÁÖÀÇ»çÇ× : // * ------------------------------------------------------------------ bool cKeyboardAgent::IsEmptyKeyboardEvent() { return mKeyEvent; } //#include "StdAfx.h" //#include "keyboardagent.h" // //cKeyboardAgent::cKeyboardAgent() //: mpDidKey(NULL) //{ // mDown = 0; // // mBufferedKeys = 0; // mBufferPos = 1; // mDown = 0; // // ZeroMemory( mKeyPress, sizeof( mKeyPress ) ); //} // //cKeyboardAgent::~cKeyboardAgent() //{ // Release(); //} // //// * ------------------------------------------------------------------ //// * ÇÔ¼öÀ̸§ : Create( HWND hWnd, LPDIRECTINPUT8 pdi ) //// * ¸ñ Àû : //// * ÁÖÀÇ»çÇ× : //// * ------------------------------------------------------------------ //HRESULT cKeyboardAgent::Create( HWND hWnd, LPDIRECTINPUT8 pDi ) //{ // HRESULT hr; // unsigned long coopFlags = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND | DISCL_NOWINKEY; // // hr = pDi->CreateDevice( GUID_SysKeyboard, &mpDidKey, NULL ); // hr |= mpDidKey->SetDataFormat( &c_dfDIKeyboard ); // hr |= mpDidKey->SetCooperativeLevel( hWnd, coopFlags ); // // DIPROPDWORD dipdw; // // dipdw.diph.dwSize = sizeof(DIPROPDWORD); // dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); // dipdw.diph.dwObj = 0; // dipdw.diph.dwHow = DIPH_DEVICE; // dipdw.dwData = KEYBOARD_BUFFER; // // hr = mpDidKey->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph ); // // mpDidKey->Acquire(); // // ClearKeyState(); // // return hr; //} // //// * ------------------------------------------------------------------ //// * ÇÔ¼öÀ̸§ : Release() //// * ¸ñ Àû : //// * ÁÖÀÇ»çÇ× : //// * ------------------------------------------------------------------ //void cKeyboardAgent::Release() //{ // if( mpDidKey ) // mpDidKey->Unacquire(); // // SAFE_RELEASE( mpDidKey ); //} // // //// * ------------------------------------------------------------------ //// * ÇÔ¼öÀ̸§ : UpdateKeyboardState() //// * ¸ñ Àû : //// * ÁÖÀÇ»çÇ× : //// * ------------------------------------------------------------------ //void cKeyboardAgent::UpdateKeyboardState() //{ // mDown = 0; // HRESULT hr = E_FAIL; // // mBufferedKeys = KEYBOARD_BUFFER; // // hr = mpDidKey->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), mDidodKey, &mBufferedKeys, 0 ); // if( FAILED( hr ) ) // { // do // { // hr = mpDidKey->Acquire(); // } // while( hr == DIERR_INPUTLOST ); // // ClearKeyState(); // } // else // { // mBufferPos = mBufferedKeys; // } //} // //// * ------------------------------------------------------------------ //// * ÇÔ¼öÀ̸§ : StartGetKeyState() //// * ¸ñ Àû : //// * ÁÖÀÇ»çÇ× : //// * ------------------------------------------------------------------ //void cKeyboardAgent::StartGetKeyState() //{ // mBufferPos = 0; //} // //// * ------------------------------------------------------------------ //// * ÇÔ¼öÀ̸§ : GetNextKeyState() //// * ¸ñ Àû : //// * ÁÖÀÇ»çÇ× : //// * ------------------------------------------------------------------ //bool cKeyboardAgent::GetNextKeyState() //{ // ++mBufferPos; // // if( mBufferPos > mBufferedKeys ) // { // return false; // } // // ///PressKey // if( mDidodKey[mBufferPos-1].dwData & 0x80 ) // { // mKeyPress[mDidodKey[mBufferPos-1].dwOfs] = true; // mDown = mDidodKey[mBufferPos-1].dwOfs; // } // else // { // mKeyPress[mDidodKey[mBufferPos-1].dwOfs] = false; // mDown = 0; // } // // return true; //} // //// * ------------------------------------------------------------------ //// * ÇÔ¼öÀ̸§ : GetKeyDown( unsigned long key ) //// * ¸ñ Àû : //// * ÁÖÀÇ»çÇ× : //// * ------------------------------------------------------------------ //bool cKeyboardAgent::GetKeyDown( unsigned long key ) //{ // if( mBufferedKeys == 0 ) // { // return false; // } // // if( mBufferPos > mBufferedKeys || mBufferPos < 1 ) // { // return false; // } // // if( ( mDidodKey[mBufferPos-1].dwOfs == key ) && ( mDidodKey[mBufferPos-1].dwData & 0x80 ) ) // { // return true; // } // // return false; //} // //// * ------------------------------------------------------------------ //// * ÇÔ¼öÀ̸§ : GetKeyUp( unsigned long key ) //// * ¸ñ Àû : //// * ÁÖÀÇ»çÇ× : //// * ------------------------------------------------------------------ //bool cKeyboardAgent::GetKeyUp( unsigned long key ) //{ // if( mBufferedKeys == 0 ) // { // return false; // } // // if( mBufferPos > mBufferedKeys || mBufferPos < 1 ) // { // return false; // } // // assert( mBufferPos <= mBufferedKeys && mBufferPos > 0 ); // // if( ( mDidodKey[mBufferPos-1].dwOfs == key ) && !( mDidodKey[mBufferPos-1].dwData & 0x80 ) ) // { // return true; // } // // return false; //} // //// * ------------------------------------------------------------------ //// * ÇÔ¼öÀ̸§ : GetKeyPressed( unsigned long key ) //// * ¸ñ Àû : //// * ÁÖÀÇ»çÇ× : //// * ------------------------------------------------------------------ //bool cKeyboardAgent::GetKeyPressed( unsigned long key ) //{ // return mKeyPress[key]? true:false; //} // //// * ------------------------------------------------------------------ //// * ÇÔ¼öÀ̸§ : ClearKeyState() //// * ¸ñ Àû : //// * ÁÖÀÇ»çÇ× : //// * ------------------------------------------------------------------ //void cKeyboardAgent::ClearKeyState() //{ // mBufferedKeys = 0; // mBufferPos = 1; // mDown = 0; // // ZeroMemory( mKeyPress, sizeof( mKeyPress ) ); //} // //// * ------------------------------------------------------------------ //// * ÇÔ¼öÀ̸§ : IsEmptyKeyboardEvent() //// * ¸ñ Àû : //// * ÁÖÀÇ»çÇ× : //// * ------------------------------------------------------------------ //bool cKeyboardAgent::IsEmptyKeyboardEvent() //{ // if( mBufferPos > mBufferedKeys ) // { // return true; // } // // return false; //}