private void internalInit() { var devs = HidFeatures.HidDevicesByUsage(HidUsagePage.Undefined); if (devs is null) { return; } foreach (var d in devs) { Devices.Add(d); } }
private void StartWatching(HidDeviceInfo d) { IntPtr h; int i = 0; if (_devThread is object) { StopWatching(); } var s = new ObservableCollection <string>(); _lastDevice = d; this.ViewingArea.ItemsSource = s; for (i = 0; i <= 255; i++) { s.Add(""); } var th = new Thread(() => { cts = new CancellationTokenSource(); h = HidFeatures.OpenHid(d); if ((long)h <= 0L) { return; } var mm = new MemPtr(65L); try { do { this.Dispatcher.Invoke(() => { for (i = 0; i <= 255; i++) { mm.LongAtAbsolute(1L) = 0L; mm.ByteAt(0L) = (byte)i; if (HidD_GetFeature(h, mm, 65)) { s[i] = "HID CODE " + i.ToString("X2") + " = " + mm.IntAtAbsolute(1L); } } }); Thread.Sleep(1000); if (cts is null || cts.IsCancellationRequested) { break; } }while (true); mm.Free(); HidFeatures.CloseHid(h); cts = null; return; } catch (ThreadAbortException) { mm.Free(); HidFeatures.CloseHid(h); cts = null; return; } catch (Exception) { mm.Free(); HidFeatures.CloseHid(h); cts = null; return; } }); th.IsBackground = true; th.SetApartmentState(ApartmentState.STA); _devThread = th; th.Start(); }