/// <summary> /// Creates new HID data reader. /// </summary> /// <param name="window">Window instance, which will reveice the WM_INPUT messages</param> public HidDataReader(Window window) { if (window == null) { throw new ArgumentNullException("window"); } this.window = window; this.window.Closed += this.OnWindowClosed; var handle = new WindowInteropHelper(this.window).Handle; var source = HwndSource.FromHwnd(handle); source.AddHook(this.WndProc); var devices = RawInputHelper.GetDevices(); int i = 0; RAWINPUTDEVICE[] rids = new RAWINPUTDEVICE[devices.Count]; // Setting handle to each rid device to receive the WM_INPUT message foreach (var device in devices) { rids[i].usUsagePage = device.UsagePage; rids[i].usUsage = device.UsageCollection; rids[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK; rids[i].hwndTarget = handle; i++; } this.handler = new HidHandler(rids); this.handler.OnHidEvent += this.OnHidEvent; }
public void Dispose() { if (this.handler != null) { this.handler.OnHidEvent -= this.OnHidEvent; this.handler.Dispose(); this.handler = null; if (this.window != null) { var windowHandle = new WindowInteropHelper(this.window).Handle; var source = HwndSource.FromHwnd(windowHandle); source.RemoveHook(this.WndProc); } } }