- 1 year ago
Identify specific HID device using windows rawinput API
To identify a specific HID (Human Interface Device) device using the Windows Raw Input API, you can follow these steps:
Register for Raw Input Device Notifications: Use the
RegisterRawInputDevices
function to register for raw input device notifications. Specify the device types you want to receive notifications for, such as keyboards or mice. You can also use theRIDEV_DEVNOTIFY
flag to receive notifications when devices are added or removed.Process Raw Input Messages: In your message loop or event handler, handle the
WM_INPUT
message to process the raw input data. Retrieve the raw input data using theGetRawInputData
function.Identify HID Devices: Parse the raw input data to identify the HID devices. Raw input data contains information about the devices, including their device type, handle, and usage page. You can use this information to determine if the raw input corresponds to an HID device.
Match Specific HID Device: Compare the attributes of the HID device from the raw input data with the desired criteria to identify the specific device you are looking for. This can include attributes like vendor ID, product ID, or usage page and usage ID.
Here's a sample code snippet in C++ that demonstrates the basic steps outlined above:
cpp#include <Windows.h>
#include <iostream>
#include <vector>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INPUT:
{
UINT dataSize;
GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, nullptr, &dataSize, sizeof(RAWINPUTHEADER));
std::vector<BYTE> buffer(dataSize);
if (GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, buffer.data(), &dataSize, sizeof(RAWINPUTHEADER)) == dataSize)
{
RAWINPUT* rawInput = reinterpret_cast<RAWINPUT*>(buffer.data());
if (rawInput->header.dwType == RIM_TYPEHID)
{
// Parse HID device information and identify the specific device
// Check the rawInput->header.hDevice and other attributes to match your desired HID device
}
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int main()
{
// Register for raw input device notifications
RAWINPUTDEVICE rid[1];
rid[0].usUsagePage = 0x01; // HID_USAGE_PAGE_GENERIC
rid[0].usUsage = 0x06; // HID_USAGE_GENERIC_KEYBOARD
rid[0].dwFlags = RIDEV_INPUTSINK;
rid[0].hwndTarget = GetConsoleWindow();
if (!RegisterRawInputDevices(rid, 1, sizeof(RAWINPUTDEVICE)))
{
std::cerr << "Failed to register raw input devices. Error code: " << GetLastError() << std::endl;
return 1;
}
// Create a window to handle raw input messages
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = L"RawInputWindowClass";
if (!RegisterClass(&wc))
{
std::cerr << "Failed to register window class. Error code: " << GetLastError() << std::endl;
return 1;
}
HWND hwnd = CreateWindow(wc.lpszClassName, L"Raw Input Window", 0, CW_USEDEFAULT, CW_USEDEFAULT,