- // CMAIN LIB - APPLICATION AND DIRECT WRAPPER
- //
- // Written by Mauricio Teichmann Ritter
- //
- // Copyright (C) 2002, Brazil. All rights reserved.
- //
- //
- // cKeyboard.cpp: implementation of the cKeyboard class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "cApplication.h"
- #include "cKeyboard.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- cKeyboard::cKeyboard()
- {
- }
- cKeyboard::~cKeyboard()
- {
- }
- BOOL cKeyboard::Create()
- {
- cInputDevice::Create();
- HRESULT hRet;
- hRet = m_lpDI->CreateDevice(GUID_SysKeyboard, &m_lpDIKeyboard, NULL);
- if FAILED(hRet) {
- Destroy();
- return FALSE;
- }
- hRet = m_lpDIKeyboard->SetDataFormat(&c_dfDIKeyboard);
- if FAILED(hRet) {
- Destroy();
- return FALSE;
- }
- hRet = m_lpDIKeyboard->SetCooperativeLevel(GetMainApp()->GetMainWnd(),
- DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
- if FAILED(hRet)
- {
- Destroy();
- return FALSE;
- }
- // Get access to the input device.
- hRet = m_lpDIKeyboard->Acquire();
- if FAILED(hRet)
- {
- Destroy();
- return FALSE;
- }
- m_KbdBuffer = (char*) malloc(256);
- return TRUE;
- }
- void cKeyboard::Destroy()
- {
- if(m_lpDIKeyboard != NULL){
- m_lpDIKeyboard->Unacquire();
- m_lpDIKeyboard->Release();
- m_lpDIKeyboard = NULL;
- }
- cInputDevice::Destroy();
- free(m_KbdBuffer);
- }
- void cKeyboard::Process()
- {
- HRESULT hRet;
- // Clear upo the buffer
- memset((LPVOID)m_KbdBuffer,0, 256);
- hRet = m_lpDIKeyboard->GetDeviceState(256,(LPVOID)m_KbdBuffer);
- if FAILED(hRet)
- {
- if(hRet == DIERR_INPUTLOST)
- {
- // Try to Reaquire;
- hRet = m_lpDIKeyboard->Acquire();
- if FAILED(hRet)
- {
- Destroy();
- return;
- }
- // Process Keyboard input again
- Process();
- }
- DXTRACE_MSG("FAILED TO RETRIEVE KEYBOARD INPUT!");
- return;
- }
- }
- BOOL cKeyboard::CheckKey(const int cKey)
- {
- //#define KEYDOWN(name, key) (name[key] & 0x80)
- char* pKbdBuffer = NULL;
- pKbdBuffer = m_KbdBuffer;
- pKbdBuffer+=cKey;
- return (*pKbdBuffer & 0x80);
- }
- LPDIRECTINPUTDEVICE8 cKeyboard::m_lpDIKeyboard = NULL;
- char* cKeyboard::m_KbdBuffer = NULL;