/// <summary> /// Registers the application to receive WM_INPUT messages for mice. /// </summary> /// <param name="hwndTarget">The application's hwnd.</param> public void RegisterForWM_INPUT(IntPtr hwndTarget) { RAWINPUTDEVICE rid = new RAWINPUTDEVICE(); rid.usUsagePage = 0x01; rid.usUsage = 0x02; //mouse rid.dwFlags = 0; // RIDEV_NOLEGACY; // adds HID mouse and also ignores legacy mouse messages rid.hwndTarget = (uint)hwndTarget.ToInt32(); //supposed to be a pointer to an array, we're only registering one device though. IntPtr pRawInputDeviceArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(RAWINPUTDEVICE))); Marshal.StructureToPtr(rid, pRawInputDeviceArray, true); uint retval = RegisterRawInputDevices(pRawInputDeviceArray, 1, (uint)Marshal.SizeOf(typeof(RAWINPUTDEVICE))); Marshal.FreeHGlobal(pRawInputDeviceArray); }
/// <summary> /// Registers the application to receive WM_INPUT messages for mice. /// </summary> /// <param name="hwndTarget">The application's hwnd.</param> public void RegisterForWM_INPUT(IntPtr hwndTarget) { // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms645565(v=vs.85).aspx RAWINPUTDEVICE rid = new RAWINPUTDEVICE(); rid.usUsagePage = 0x01; rid.usUsage = 0x02; // Mouse rid.dwFlags = RIDEV_INPUTSINK; rid.hwndTarget = hwndTarget; // Supposed to be a pointer to an array, we're only registering one device though IntPtr pRawInputDeviceArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(RAWINPUTDEVICE))); Marshal.StructureToPtr(rid, pRawInputDeviceArray, true); uint retval = RegisterRawInputDevices(pRawInputDeviceArray, 1, (uint)Marshal.SizeOf(typeof(RAWINPUTDEVICE))); Marshal.FreeHGlobal(pRawInputDeviceArray); }