/// <summary> /// Function to unhook raw input from the application. /// </summary> private void UnhookRawInput() { foreach (HIDUsage type in _devices.Values.Select(item => item.DeviceUsage).Distinct()) { RawInputApi.UnregisterRawInputDevice(type); } }
/// <summary> /// Function to unregister the device from the raw input provider. /// </summary> /// <param name="device">The device to unregister from the raw input provider.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="device"/> parameter is <b>null</b>.</exception> /// <remarks> /// This will unregister a previously registered <see cref="IGorgonRawInputDevice"/>. When the last device of a specific type (e.g. a mouse, keyboard, etc...) is unregistered, then the /// Raw Input messages for that device type will also be unregistered and the application will no longer receive messages from that type of device. /// </remarks> public void UnregisterDevice(IGorgonRawInputDevice device) { if (device == null) { throw new ArgumentNullException(nameof(device)); } lock (_syncLock) { var key = new DeviceKey { DeviceType = device.DeviceType, DeviceHandle = device.Handle }; switch (device.DeviceType) { case RawInputType.Keyboard: _keyboardDevices.Remove(key); break; case RawInputType.Mouse: _mouseDevices.Remove(key); break; case RawInputType.HID: _hids.Remove(key); break; default: throw new ArgumentException(string.Format(Resources.GORINP_RAW_ERR_UNKNOWN_DEVICE_TYPE, device.DeviceType), nameof(device)); } if (!_devices.ContainsKey(key)) { return; } _devices.Remove(key); // If all devices of this type have been unregistered, unregister with raw input as well. if ((_devices.Count(item => item.Value.DeviceType == device.DeviceType) == 0) && (RawInputApi.GetDeviceRegistration(device.DeviceUsage) != null)) { RawInputApi.UnregisterRawInputDevice(device.DeviceUsage); } // If we have no more registered devices, then uninstall the filter. if (_devices.Count != 0) { return; } _filter?.Dispose(); _filter = null; } }