📄 dxinput.cpp
字号:
/*
dxinput.cpp - Input device handle
Copyright (c) HalfLucifer, 2001.7.20
*/
#include "dxinput.h"
DXinput::~DXinput()
{
if (lpDI != NULL)
{
if (lpDIKeyboard != NULL)
{
lpDIKeyboard->Unacquire();
lpDIKeyboard->Release();
lpDIKeyboard = NULL;
}
lpDI->Release();
lpDI = NULL;
}
}
bool DXinput::Initialize(void)
{
HRESULT hr;
hr = (DirectInput8Create(hInstance, DIRECTINPUT_VERSION, // Create the main DirectInput object
IID_IDirectInput8, (void**)&lpDI, NULL));
if (FAILED(hr))
{
MessageBox(NULL, "Could not create main DInput object", "ERROR", MB_OK);
return false;
}
hr = (lpDI->CreateDevice(GUID_SysKeyboard, &lpDIKeyboard, NULL)); // Create the keyboard's device object
if (FAILED(hr))
{
MessageBox(NULL, "Could not create keyboard's object", "ERROR", MB_OK);
DXinput::~DXinput();
return false;
}
hr = (lpDIKeyboard->SetDataFormat(&c_dfDIKeyboard)); // Set the keyboard's data format
if (FAILED(hr))
{
MessageBox(NULL, "Could not set keyboard's data format", "ERROR", MB_OK);
DXinput::~DXinput();
return false;
}
hr = (lpDIKeyboard->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)); // Set the keyboard's cooperation level
if (FAILED(hr))
{
MessageBox(NULL, "Could not set keyboard's cooperation level", "ERROR", MB_OK);
DXinput::~DXinput();
return false;
}
lpDIKeyboard->Acquire(); // Acquire the keyboard for use
return true;
}
void DXinput::Update(void)
{
HRESULT hr;
hr = (lpDIKeyboard->GetDeviceState(sizeof(unsigned char[256]), (LPVOID)&KeyBuffer)); // Check if keyboard still aquired
if (FAILED(hr))
{
if (hr == DIERR_INPUTLOST)
{
hr = (lpDIKeyboard->Acquire()); // Try to re-acquire the keyboard
if (FAILED(hr))
{
MessageBox(NULL, "Keyboard has been lost", "ERROR", MB_OK);
DXinput::~DXinput();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -