📄 inputmgrsys_t.cpp
字号:
// InputMgrSys_t.cpp: implementation of the InputMgrSys_t class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "InputMgrSys_t.h"
#define BUFFER_SIZE 256
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
InputMgrSys_t::InputMgrSys_t()
{
m_pDI = 0;
m_pDIMouse = 0;
m_pDIKeyboard = 0;
m_JustPressedKeys.clear();
m_JustUnPressedKeys.clear();
m_PressedKeys.clear();
}
InputMgrSys_t::~InputMgrSys_t()
{
_RELEASE(m_pDIMouse);
_RELEASE(m_pDIKeyboard);
_RELEASE(m_pDI);
}
bool InputMgrSys_t::Create()
{
HRESULT hr;
hr = DirectInput8Create((HINSTANCE)GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8,
(void**)&m_pDI, NULL);
if (SUCCEEDED(hr))
{
return true;
}
return false;
}
bool InputMgrSys_t::CreateKeyboardDevice()
{
HRESULT hr;
hr = m_pDI->CreateDevice(GUID_SysKeyboard, &m_pDIKeyboard, NULL);
if (FAILED(hr))
{
return false;
}
hr = m_pDIKeyboard->SetDataFormat(&c_dfDIKeyboard);
if (FAILED(hr))
{
return false;
}
return true;
}
bool InputMgrSys_t::SetKeyboardBehavior(void *hWnd, unsigned long dwBehavior)
{
HRESULT hr;
DIPROPDWORD theBufferData;
// Set the cooperative level
hr = m_pDIKeyboard->SetCooperativeLevel((HWND)hWnd, dwBehavior);
if (FAILED(hr))
{
return false;
}
ZeroMemory(&theBufferData,sizeof(DIPROPDWORD));
theBufferData.diph.dwSize = sizeof(DIPROPDWORD);
theBufferData.diph.dwHeaderSize = sizeof(DIPROPHEADER);
theBufferData.diph.dwObj = 0;
theBufferData.diph.dwHow = DIPH_DEVICE;
theBufferData.dwData = BUFFER_SIZE;
/* set the size of the keyboard buffer */
hr = m_pDIKeyboard->SetProperty(DIPROP_BUFFERSIZE, &theBufferData.diph);
if (FAILED(hr))
{
return false;
}
hr = m_pDIKeyboard->Acquire();
if (FAILED(hr))
{
return false;
}
return true;
}
bool InputMgrSys_t::CreateMouseDevice()
{
HRESULT hr;
hr = m_pDI->CreateDevice(GUID_SysMouse, &m_pDIMouse, NULL);
if (FAILED(hr))
{
return false;
}
hr = m_pDIMouse->SetDataFormat(&c_dfDIMouse2);
if (FAILED(hr))
{
return false;
}
return true;
}
bool InputMgrSys_t::SetMouseBehavior(void *hWnd, unsigned long dwBehavior)
{
HRESULT hr;
DIPROPDWORD theBufferData;
// Set the cooperative level
hr = m_pDIMouse->SetCooperativeLevel((HWND)hWnd, dwBehavior);
if (FAILED(hr))
{
return false;
}
ZeroMemory(&theBufferData,sizeof(DIPROPDWORD));
theBufferData.diph.dwSize = sizeof(DIPROPDWORD);
theBufferData.diph.dwHeaderSize = sizeof(DIPROPHEADER);
theBufferData.diph.dwObj = 0;
theBufferData.diph.dwHow = DIPH_DEVICE;
theBufferData.dwData = BUFFER_SIZE;
/* set the size of the Mouse buffer */
hr = m_pDIMouse->SetProperty(DIPROP_BUFFERSIZE, &theBufferData.diph);
if (FAILED(hr))
{
return false;
}
hr = m_pDIMouse->Acquire();
if (FAILED(hr))
{
return false;
}
return true;
}
bool InputMgrSys_t::UpdateKeyboard()
{
KeyVector_t::iterator theIterator;
DIDEVICEOBJECTDATA theBufferedKeys[BUFFER_SIZE];
DWORD theNumKeys = BUFFER_SIZE;
HRESULT theHResult;
unsigned long theLoop;
/* make sure we have a direct input interface */
if (m_pDIKeyboard)
{
/* clear the list of just pressed and unpressed keys */
m_JustPressedKeys.clear();
m_JustUnPressedKeys.clear();
/* query the direct input buffer */
theHResult = m_pDIKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), theBufferedKeys, &theNumKeys, 0);
if (SUCCEEDED(theHResult))
{
if (theHResult == DI_BUFFEROVERFLOW)
{
return false;
}
/* loop over the number of key inputs */
for (theLoop = 0; theLoop < theNumKeys; theLoop++)
{
Key theTempKey;
ISModifierState theTempModifier;
/* wrap the key in an Key object */
theTempKey.m_TimeStamp = theBufferedKeys[theLoop].dwTimeStamp;
theTempKey.m_ScanCode = theBufferedKeys[theLoop].dwOfs;
theTempKey.m_Modifier = m_ModifierState;
/* check if the key is a modifer */
theTempModifier = GetModifierState(theBufferedKeys[theLoop].dwOfs);
/* is the key is down? */
if (theBufferedKeys[theLoop].dwData & 0x80)
{
/* set the modifier state if necessary */
if (theTempModifier != kismsNone)
m_ModifierState |= theTempModifier;
/* this key was just pressed... */
m_JustPressedKeys.push_back(theTempKey);
/* ...as well as "pressed" */
theIterator = std::find(m_PressedKeys.begin(), m_PressedKeys.end(), theTempKey);
if (theIterator == m_PressedKeys.end())
m_PressedKeys.push_back(theTempKey);
}
else
{
/* set the modifier state if necessary */
if (theTempModifier != kismsNone)
{
m_ModifierState ^= theTempModifier;
theTempKey.m_Modifier ^= theTempModifier;
}
/* this key was just released */
m_JustUnPressedKeys.push_back(theTempKey);
/* erase this key from the pressed list, if we can find it. */
theIterator = m_PressedKeys.begin();
while (theIterator != m_PressedKeys.end())
{
if ((*theIterator).m_ScanCode == theTempKey.m_ScanCode)
{
m_PressedKeys.erase(theIterator);
}
else
theIterator++;
}
}
}
}
else
{
if ((theHResult == DIERR_INPUTLOST) ||
(theHResult == DIERR_NOTACQUIRED))
{
/* we lost the device, reaquire it. */
theHResult = m_pDIKeyboard->Acquire();
}
}
}
return (SUCCEEDED(theHResult));
}
unsigned char InputMgrSys_t::GetModifierState(unsigned char nScanCode)
{
ISModifierState theState = kismsNone;
if ((nScanCode == DIK_LCONTROL) ||
(nScanCode == DIK_RCONTROL))
{
theState = kismsCtrl;
}
else
if ((nScanCode == DIK_LMENU) ||
(nScanCode == DIK_RMENU))
{
theState = kismsAlt;
}
else
if ((nScanCode == DIK_LSHIFT) ||
(nScanCode == DIK_RSHIFT))
{
theState = kismsShift;
}
return theState;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -