Skip to content

Instantly share code, notes, and snippets.

@Meigyoku-Thmn
Last active June 20, 2023 07:37
Show Gist options
  • Save Meigyoku-Thmn/ae9b2bb82b27211b8b8cc6e9f39b5736 to your computer and use it in GitHub Desktop.
Save Meigyoku-Thmn/ae9b2bb82b27211b8b8cc6e9f39b5736 to your computer and use it in GitHub Desktop.
WM_INPUT
auto className = "Window Class";
WNDCLASSA windowClass = {
.lpfnWndProc = WndProc,
.hInstance = GetModuleHandleA(nullptr),
.lpszClassName = className,
};
RegisterClassA(&windowClass);
auto hwnd = CreateWindowExA(0, className, "Window Name", 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0);
RAWINPUTDEVICE device{
.usUsagePage = HID_USAGE_PAGE_GENERIC,
.usUsage = HID_USAGE_GENERIC_MOUSE,
.dwFlags = RIDEV_INPUTSINK,
.hwndTarget = hwnd,
};
RegisterRawInputDevices(&device, 1, sizeof(device));
LRESULT CALLBACK WndProc(HWND hwnd, UINT event, WPARAM wparam, LPARAM lparam) {
switch (event) {
case WM_INPUT: {
static struct {
int dx = 0;
int dy = 0;
} mouseInput;
static vector<BYTE> buffer;
UINT size{};
GetRawInputData((HRAWINPUT)lparam, RID_INPUT, nullptr, &size, sizeof(RAWINPUTHEADER));
buffer.reserve(size);
auto raw = (RAWINPUT*)buffer.data();
GetRawInputData((HRAWINPUT)lparam, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER));
if (raw->header.dwType == RIM_TYPEMOUSE) {
mouseInput.dx = raw->data.mouse.lLastX;
mouseInput.dy = raw->data.mouse.lLastY;
}
return 0;
}
}
return DefWindowProcA(hwnd, event, wparam, lparam);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment