Microsoft DirectX 库包含一个可用于获取键盘状态的函数。它是 DirectInput API 的一部分。下面的代码演示了如何轮询键盘以获取按键状态信息。必须添加额外的逻辑来检测何时按下/释放键并将其转换为字符。
请注意,这需要 Microsoft DirectX SDK 进行编译。
//Public domain: no attribution required
#include "stdafx.h"
#include "dxlog.h"
#pragma comment(lib, "dinput8")
#pragma comment(lib, "dxguid")
LPDIRECTINPUT8 din;
LPDIRECTINPUTDEVICE8 dinkbd;
BYTE keystate[256];
DIDATAFORMAT dfi;
void init_dinput(HINSTANCE hInst, HWND hWnd)
{
HRESULT hr;
hr = DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&din, NULL);
hr = din->CreateDevice(GUID_SysKeyboard, &dinkbd, NULL);
hr = dinkbd->SetDataFormat(&c_dfDIKeyboard);
// share the keybdb and collect even when not the active application
hr = dinkbd->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
}
void detect_input(void)
{
dinkbd->Acquire();
dinkbd->GetDeviceState(256, keystate);
}
void clean_dinput(void)
{
dinkbd->Unacquire();
din->Release();
}
void print_state()
{
WCHAR pState[4096] = L"";
WCHAR temp[32];
for (int i = 0; i < 256; i++)
{
if (keystate[i] != 0)
{
wsprintf (temp, L"%d(%d) ", i, keystate[i]);
lstrcat(pState, temp);
}
}
if (lstrlen(pState) != 0)
{
lstrcat(pState, L"\n");
OutputDebugString(pState);
}
}