/// <summary> /// Registers the devices that supply the raw input data. /// </summary> /// <param name="usagePage">The usage page.</param> /// <param name="usageId">The usage id.</param> /// <param name="flags">The flags.</param> /// <param name="target">The target.</param> /// <param name="options">The options.</param> public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, RegisterDeviceOptions options = RegisterDeviceOptions.Default) { var rawInputDevices = new RawInputDevice[1]; rawInputDevices[0].UsagePage = (short)usagePage; rawInputDevices[0].Usage = (short)usageId; rawInputDevices[0].Flags = (int)flags; rawInputDevices[0].Target = target; // Register this device RawInputFunctions.RegisterRawInputDevices(rawInputDevices, 1, Utilities.SizeOf <RawInputDevice>()); if (options != RegisterDeviceOptions.NoFiltering && rawInputMessageFilter == null) { rawInputMessageFilter = new RawInputMessageFilter(); if (options == RegisterDeviceOptions.Default) { Application.AddMessageFilter(rawInputMessageFilter); } else { MessageFilterHook.AddMessageFilter(target, rawInputMessageFilter); } } }
// This allocates, however it should be used only when launching app public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target) { Device.RegisterDevice(usagePage, usageId, flags, target, false); if (m_filter == null) { m_filterWindow = target; m_filter = new RawInputMessageFilter(); MessageFilterHook.AddMessageFilter(m_filterWindow, m_filter); } }
public DisposableRawInputHook(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, EventHandler <KeyboardInputEventArgs> handler) { this.usagePage = usagePage; this.usageId = usageId; this.handler = handler; Device.RegisterDevice(usagePage, usageId, flags, target); Device.KeyboardInput += handler; }
public static bool RegisterDevice(UsagePage usagePage, UsageId usageId, ModeFlags flags, IntPtr target) { var device = new RawInputDevice { UsagePage = (ushort)usagePage, Usage = (ushort)usageId, Flags = (uint)flags, Target = target }; return(RegisterDevice(device)); }
public DisposableRawInputHook(UsagePage usagePage, UsageId usageIdKeyboard, UsageId usageIdMouse, DeviceFlags flags, IntPtr target, EventHandler <KeyboardInputEventArgs> keyHandler, EventHandler <MouseInputEventArgs> mouseHandler) { this.usagePage = usagePage; this.usageIdKeyboard = usageIdKeyboard; this.usageIdMouse = usageIdMouse; this.keyHandler = keyHandler; this.mouseHandler = mouseHandler; Device.RegisterDevice(usagePage, usageIdKeyboard, flags, target); Device.KeyboardInput += keyHandler; Device.RegisterDevice(usagePage, usageIdMouse, flags, target); Device.MouseInput += mouseHandler; }
/// <summary> /// Registers a device that supplies the raw input data. /// </summary> /// <param name="usagePage">The usage page.</param> /// <param name="usageId">The usage id.</param> /// <param name="flags">A combination of flags specifying how to interpret the values of <paramref name="usageId"/> and <paramref name="usagePage"/>.</param> /// <param name="windowHandle"> /// A handle to the target window. Specify <see cref="IntPtr.Zero"/> to not associate the device with a window. Instead the /// input data will follow the input focus. This is useful when specifying the flag <see cref="DeviceFlags.InputSink"/> or /// <see cref="DeviceFlags.ExclusiveInputSink"/>. The default value is <see cref="IntPtr.Zero"/>. /// </param> /// <remarks> /// To receive <c>WM_INPUT</c> messages (through the events <see cref="MouseInput"/>, <see cref="KeyboardInput"/>, and /// <see cref="Input"/>), an application must first register the raw input devices using <see cref="RegisterRawInputDevice"/>. /// By default, an application does not receive raw input. /// <para/> /// To receive <c>WM_INPUT_DEVICE_CHANGE</c> messages (through the <see cref="DeviceChanged"/> event), an application must /// specify <see cref="DeviceFlags.DeviceNotify"/> for each device class that is specified by the <see cref="UsagePage"/> /// and <see cref="UsageId"/> of the <see cref="RawInputDevice" /> structure. By default, an application does not receive /// <c>WM_INPUT_DEVICE_CHANGE</c> notifications for raw input device arrival and removal. /// <para/> /// To unregister a device, specify <see cref="DeviceFlags.Remove"/>. This tells the operating system to stop reading /// from a device that matches the top level collection (specified through <paramref name="usagePage"/> and /// <paramref name="usageId"/>). Alternatively, use the <see cref="UnregisterDevice"/> method. /// <para/> /// If the <see cref="DeviceFlags.Remove"/> flag is set and <paramref name="windowHandle"/> is not set to /// <see cref="IntPtr.Zero"/>, then parameter validation will fail. /// </remarks> public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr windowHandle = default) { RawInputDevice rawInputDevice; rawInputDevice.UsagePage = usagePage; rawInputDevice.UsageId = usageId; rawInputDevice.Flags = flags; rawInputDevice.Target = windowHandle; if (!RegisterRawInputDevice(rawInputDevice)) { CheckLastResult(); } UpdateRegisteredDeviceList(); }
/// <summary> /// Registers the devices that supply the raw input data. /// </summary> /// <param name="usagePage">The usage page.</param> /// <param name="usageId">The usage id.</param> /// <param name="flags">The flags.</param> public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags) { RegisterDevice(usagePage, usageId, flags, IntPtr.Zero); }
/// <summary> /// Registers the devices that supply the raw input data. /// </summary> /// <param name="usagePage">The usage page.</param> /// <param name="usageId">The usage id.</param> /// <param name="flags">The flags.</param> /// <param name="target">The target.</param> /// <param name="options">The options.</param> public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, RegisterDeviceOptions options = RegisterDeviceOptions.Default) { var rawInputDevices = new RawInputDevice[1]; rawInputDevices[0].UsagePage = (short)usagePage; rawInputDevices[0].Usage = (short)usageId; rawInputDevices[0].Flags = (int)flags; rawInputDevices[0].Target = target; // Register this device RawInputFunctions.RegisterRawInputDevices(rawInputDevices, 1, Utilities.SizeOf<RawInputDevice>()); if (options != RegisterDeviceOptions.NoFiltering && rawInputMessageFilter == null) { rawInputMessageFilter = new RawInputMessageFilter(); if (options == RegisterDeviceOptions.Default) { Application.AddMessageFilter(rawInputMessageFilter); } else { MessageFilterHook.AddMessageFilter(target, rawInputMessageFilter); } } }
/// <summary> /// Registers the devices that supply the raw input data. /// </summary> /// <param name="usagePage">The usage page.</param> /// <param name="usageId">The usage id.</param> /// <param name="flags">The flags.</param> /// <param name="target">The target.</param> /// <param name="addMessageFilter">if set to <c>true</c> register message filter to Application.AddMessageFilter.</param> public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, bool addMessageFilter = true) { if (target == IntPtr.Zero) { target = Win32Native.GetFocus(); } var rawInputDevices = new RawInputDevice[1]; rawInputDevices[0].UsagePage = (short) usagePage; rawInputDevices[0].Usage = (short) usageId; rawInputDevices[0].Flags = (int) flags; rawInputDevices[0].Target = target; // Register this device RawInputFunctions.RegisterRawInputDevices(rawInputDevices, 1, Utilities.SizeOf<RawInputDevice>()); if (rawInputMessageFilter == null && addMessageFilter) { rawInputMessageFilter = new RawInputMessageFilter(); MessageFilterHook.AddMessageFilter(target, rawInputMessageFilter); } }
/// <summary> /// Registers the devices that supply the raw input data. /// </summary> /// <param name="usagePage">The usage page.</param> /// <param name="usageId">The usage id.</param> /// <param name="flags">The flags.</param> /// <param name="target">The target.</param> /// <param name="addMessageFilter">if set to <c>true</c> register message filter to Application.AddMessageFilter.</param> public static void RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, bool addMessageFilter = true) { var rawInputDevices = new RawInputDevice[1]; rawInputDevices[0].UsagePage = (short) usagePage; rawInputDevices[0].Usage = (short) usageId; rawInputDevices[0].Flags = (int) flags; rawInputDevices[0].Target = target; // Register this device RawInputFunctions.RegisterRawInputDevices(rawInputDevices, 1, Utilities.SizeOf<RawInputDevice>()); if (rawInputMessageFilter == null && addMessageFilter) { rawInputMessageFilter = new RawInputMessageFilter(); Application.AddMessageFilter(rawInputMessageFilter); } }
/// <summary> /// Unregisters a device that supplies the raw input data. /// </summary> /// <param name="usagePage">The usage page.</param> /// <param name="usageId">The usage id.</param> /// <remarks> /// This function tells the operating system to stop reading from a device that matches the top level collection /// (specified through <paramref name="usagePage"/> and <paramref name="usageId"/>). /// </remarks> public static void UnregisterDevice(UsagePage usagePage, UsageId usageId) => RegisterDevice(usagePage, usageId, DeviceFlags.Remove);