public void SearchDevices()
 {
     lock (lockObject)
     {
         var           local     = DeviceList.Local;
         var           devices   = local.GetDevices(DeviceTypes.Hid).OfType <HidDevice>();
         List <string> instances = new List <string>();
         foreach (var device in devices)
         {
             if (ignoredDeviceService.IsIgnored(device.DevicePath))
             {
                 continue;
             }
             string uniqueId = IdHelper.GetUniqueId(device.DevicePath);
             instances.Add(uniqueId);
             if (currentDevices.Any(d => d.UniqueId == uniqueId))
             {
                 continue;
             }
             try
             {
                 HidStream hidStream;
                 if (device.TryOpen(out hidStream))
                 {
                     hidStream.ReadTimeout = Timeout.Infinite;
                     var reportDescriptor = device.GetReportDescriptor();
                     var deviceItems      = reportDescriptor.DeviceItems.Where(i => Match(i,
                                                                                          Usage.GenericDesktopGamepad, Usage.GenericDesktopJoystick, Usage.GenericDesktopMultiaxisController));
                     foreach (var deviceItem in deviceItems)
                     {
                         var inputDevice = new RawInputDevice(device, reportDescriptor, deviceItem, hidStream, uniqueId);
                         var config      = inputConfigManager.LoadConfig(inputDevice.UniqueId);
                         inputDevice.InputConfiguration = config;
                         currentDevices.Add(inputDevice);
                         Connected?.Invoke(this, new DeviceConnectedEventArgs(inputDevice));
                     }
                 }
             }
             catch (Exception)
             {
             }
         }
         foreach (var device in currentDevices.ToArray())
         {
             string guid = device.UniqueId;
             if (!instances.Any(i => i == guid))
             {
                 currentDevices.Remove(device);
                 device.Dispose();
                 Disconnected?.Invoke(this, new DeviceDisconnectedEventArgs(device));
             }
         }
     }
 }
Beispiel #2
0
        public static void RegisterDeviceHook(IntPtr handle)
        {
            var subscription = new RawInputDevice[] {
                new RawInputDevice {
                    usUsage = 0x04, usUsagePage = 0x01, dwFlags = RawInputDeviceFlags.InputSink, hwndTarget = handle
                },
                new RawInputDevice {
                    usUsage = 0x05, usUsagePage = 0x01, dwFlags = RawInputDeviceFlags.InputSink, hwndTarget = handle
                },
                new RawInputDevice {
                    usUsage = 0x08, usUsagePage = 0x01, dwFlags = RawInputDeviceFlags.InputSink, hwndTarget = handle
                },
            };
            var result = RegisterRawInputDevices(subscription, (uint)subscription.Length, (uint)Marshal.SizeOf <RawInputDevice>());

            if (!result)
            {
                throw new InvalidOperationException("Failed to register hid device hook");
            }
        }