Last active
October 26, 2023 17:24
-
-
Save luluco250/76ec751d30e5574a6602070cd109d9e2 to your computer and use it in GitHub Desktop.
Win32 Mouse Raw Input using wxWidgets for output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <wx/wxprec.h> | |
#ifndef WX_PRECOMP | |
#include <wx/wx.h> | |
#endif | |
#define NOMINMAX | |
#define WIN32_LEAN_AND_MEAN | |
#define VC_EXTRALEAN | |
#include <Windows.h> | |
#ifndef HID_USAGE_PAGE_GENERIC | |
#define HID_USAGE_PAGE_GENERIC USHORT(0x01) | |
#endif | |
#ifndef HID_USAGE_GENERIC_MOUSE | |
#define HID_USAGE_GENERIC_MOUSE USHORT(0x02) | |
#endif | |
class Frame : public wxFrame { | |
public: | |
wxStaticText* text; | |
wxTimer* timer; | |
struct { | |
long x, y, wheel; | |
} mousedelta; | |
Frame() : | |
wxFrame( | |
nullptr, | |
wxID_ANY, | |
"", | |
wxDefaultPosition, | |
{85, 103}, | |
wxCAPTION | wxCLOSE_BOX | wxSTAY_ON_TOP | |
) { | |
auto main_sizer = new wxBoxSizer(wxVERTICAL); | |
text = new wxStaticText( | |
this, | |
wxID_ANY, | |
"0", | |
wxDefaultPosition, | |
wxDefaultSize, | |
0 | |
); | |
text->SetFont(wxFont(12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL)); | |
main_sizer->Add(text, 0, wxALL | wxEXPAND, 5); | |
SetSizer(main_sizer); | |
Center(); | |
timer = new wxTimer(this); | |
timer->Start(30); | |
Bind(wxEVT_TIMER, &Frame::OnUpdate, this, timer->GetId()); | |
RAWINPUTDEVICE rid[1]; | |
rid[0].usUsagePage = HID_USAGE_PAGE_GENERIC; | |
rid[0].usUsage = HID_USAGE_GENERIC_MOUSE; | |
rid[0].dwFlags = RIDEV_INPUTSINK; | |
rid[0].hwndTarget = GetHWND(); | |
RegisterRawInputDevices(rid, 1, sizeof(rid[0])); | |
} | |
~Frame() { | |
delete text; | |
delete timer; | |
} | |
void OnUpdate(wxTimerEvent& e) { | |
wxString str = "X: "; | |
str << mousedelta.x; | |
str << "\nY: " << mousedelta.y; | |
str << "\nWheel: " << mousedelta.wheel; | |
text->SetLabel(str); | |
mousedelta.x = mousedelta.y = mousedelta.wheel = 0; | |
} | |
WXLRESULT MSWWindowProc(WXUINT message, WXWPARAM wparam, WXLPARAM lparam) { | |
if (message == WM_INPUT) { | |
unsigned size = sizeof(RAWINPUT); | |
static RAWINPUT raw[sizeof(RAWINPUT)]; | |
GetRawInputData((HRAWINPUT)lparam, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)); | |
if (raw->header.dwType == RIM_TYPEMOUSE) { | |
mousedelta.x = raw->data.mouse.lLastX; | |
mousedelta.y = raw->data.mouse.lLastY; | |
if (raw->data.mouse.usButtonFlags & RI_MOUSE_WHEEL) | |
mousedelta.wheel = static_cast<short>(raw->data.mouse.usButtonData) / WHEEL_DELTA; | |
} | |
return 0; | |
} | |
return wxFrame::MSWWindowProc(message, wparam, lparam); | |
} | |
}; | |
class Main : public wxApp { | |
public: | |
Frame* frame; | |
bool OnInit() { | |
frame = new Frame(); | |
frame->Show(); | |
return true; | |
} | |
}; | |
wxIMPLEMENT_APP(Main); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment