Example #1
0
 public RawInputDeviceRegistration(ushort usagePage, ushort usage, RawInputDeviceFlags flags, IntPtr hWndTarget)
 {
     usUsagePage = usagePage;
     usUsage     = usage;
     dwFlags     = flags;
     hwndTarget  = hWndTarget;
 }
Example #2
0
 private RawInputDevice(ushort usage, RawInputDeviceFlags flags, IntPtr target, HidPage usagePage)
 {
     Usage     = usage;
     Flags     = flags;
     Target    = target;
     UsagePage = usagePage;
 }
Example #3
0
        /// <summary>
        /// RAWINPUT 이벤트 수신 시작
        /// </summary>
        private void StartEventCapture()
        {
            RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1];

            // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-tagrawinputdevice
            // 이 옵션을 설정하면 발신자가 포 그라운드에 있지 않아도 발신자가 입력을받을 수 있습니다.
            // 참고 hwndTarget을 지정해야 합니다.

            //-------------------------------------------------------
            // RIM_INPUT = FOREGROUND에서만 WM_INPUT 이벤트 수신
            // RIDEV_INPUTSINK = BACKGROUND에서도 WM_INPUT 이벤트 수신
            //-------------------------------------------------------
            RawInputDeviceFlags flags = RawInputDeviceFlags.RIDEV_INPUTSINK;
            {
                rid[0].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
                rid[0].usUsage     = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
                rid[0].dwFlags     = flags;
                rid[0].hwndTarget  = this.Handle;
            }

            uint size = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(RAWINPUTDEVICE));

            if (Function.RegisterRawInputDevices(rid, 1, size) == false)
            {
                Console.WriteLine("ResigterRawInputDevices Error: {0}", Marshal.GetLastWin32Error());
            }
            else
            {
                Console.WriteLine("Registration of device was successful");
            }
        }
Example #4
0
        /// <summary>
        ///     Registers the device.
        /// </summary>
        /// <param name="hidUsagePage">        The HID usage page. </param>
        /// <param name="hidUsage">            The HID usage. </param>
        /// <param name="rawInputDeviceFlags"> The raw input device flags. </param>
        /// <param name="hWndTarget">          The window target. </param>
        public static void RegisterDevice(HIDUsagePage hidUsagePage,
                                          HIDUsage hidUsage,
                                          RawInputDeviceFlags rawInputDeviceFlags,
                                          IntPtr hWndTarget)
        {
            RAWINPUTDEVICE[]? rawInputDevices = new RAWINPUTDEVICE[1];
            rawInputDevices[0].UsagePage      = hidUsagePage;
            rawInputDevices[0].Usage          = hidUsage;
            rawInputDevices[0].Flags          = rawInputDeviceFlags;
            rawInputDevices[0].WindowHandle   = hWndTarget;

            if (!User32.RegisterRawInputDevices(rawInputDevices, 1, Marshal.SizeOf <RAWINPUTDEVICE>()))
            {
                throw new Win32Exception(Kernel32.GetLastError(), $"{nameof(User32.RegisterRawInputDevices)} failed!");
            }
        }
Example #5
0
        /// <summary>
        /// RAWINPUT 이벤트 수신 중지
        /// </summary>
        private void EndEventCapture()
        {
            RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1];

            RawInputDeviceFlags flags = RawInputDeviceFlags.RIDEV_REMOVE;
            {
                rid[0].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
                rid[0].usUsage     = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
                rid[0].dwFlags     = flags;         // here!
                rid[0].hwndTarget  = IntPtr.Zero;   // here!
            }

            //De-register
            uint size = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(RAWINPUTDEVICE));

            if (Function.RegisterRawInputDevices(rid, 1, size) == false)
            {
                Console.WriteLine("De-ResigterRawInputDevices Error: {0}", Marshal.GetLastWin32Error());
            }
            else
            {
                Console.WriteLine("De-Registration of device was successful");
            }
        }
        /// <summary>
        /// Function to register the device with the raw input provider.
        /// </summary>
        /// <param name="device">The device to register with the raw input provider.</param>
        /// <param name="settings">[Optional] Settings for the device type.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="device"/> parameter is <b>null</b>.</exception>
        /// <remarks>
        /// <para>
        /// This will register the <see cref="IGorgonRawInputDevice"/> with the application. For the very first device of a specific type (e.g. a mouse, keyboard, etc...) the Raw Input object will set up
        /// the device type registration for the device. This enables an application to start receiving Raw Input messages from a device type.
        /// </para>
        /// <para>
        /// The optional <paramref name="settings"/> parameter allows an application change how raw input handles the device being registered. It can be used to set up background input monitoring, or a
        /// target window for raw input messages (which must be set if the background option is turned on). By default, there is no background message processing and no target window (messages go to
        /// whichever window has focus).
        /// </para>
        /// <para>
        /// Every call to this method should be paired with a call to <see cref="UnregisterDevice"/> when the device(s) are no longer needed.
        /// </para>
        /// </remarks>
        public void RegisterDevice(IGorgonRawInputDevice device, GorgonRawInputSettings?settings = null)
        {
            IntPtr targetHandle = IntPtr.Zero;

            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (settings == null)
            {
                settings = new GorgonRawInputSettings();
            }

            lock (_syncLock)
            {
                // If we've not set up the filter yet, then add it to the window now.
                if (_filter == null)
                {
                    _filter = new RawInputMessageFilter(_keyboardDevices, _mouseDevices, _hids, _applicationWindow, _useNativeHook);
                }

                var key = new DeviceKey
                {
                    DeviceType   = device.DeviceType,
                    DeviceHandle = device.Handle
                };

                if (_devices.ContainsKey(key))
                {
                    return;
                }

                switch (device.DeviceType)
                {
                case RawInputType.Keyboard:
                    _keyboardDevices.Add(key, (IRawInputDeviceData <GorgonRawKeyboardData>)device);
                    break;

                case RawInputType.Mouse:
                    _mouseDevices.Add(key, (IRawInputDeviceData <GorgonRawMouseData>)device);
                    break;

                case RawInputType.HID:
                    _hids.Add(key, (IRawInputDeviceData <GorgonRawHIDData>)device);
                    break;

                default:
                    throw new ArgumentException(string.Format(Resources.GORINP_RAW_ERR_UNKNOWN_DEVICE_TYPE, device.DeviceType), nameof(device));
                }

                _devices.Add(key, device);

                // Get the current device registration properties.
                RAWINPUTDEVICE?deviceReg = RawInputApi.GetDeviceRegistration(device.DeviceUsage);
                if (deviceReg != null)
                {
                    // Remove the device before updating it.
                    UnregisterDevice(device);
                }

                // If we omit the target window, and specify background messages, we'll use the application window instead.
                // This is because Raw Input requires that background devices have a window target.
                if ((settings.Value.TargetWindow.IsNull) && (settings.Value.AllowBackground))
                {
                    unsafe
                    {
                        settings = new GorgonRawInputSettings
                        {
                            TargetWindow    = new GorgonReadOnlyPointer((void *)_applicationWindow, IntPtr.Size),
                            AllowBackground = true
                        };
                    }
                }

                RawInputDeviceFlags flags = RawInputDeviceFlags.None;

                if (settings.Value.AllowBackground)
                {
                    flags |= RawInputDeviceFlags.InputSink;
                }

                RawInputApi.RegisterRawInputDevice(device.DeviceUsage, targetHandle, flags);
            }
        }
Example #7
0
 public RawInputDeviceRegistration(HidUsageAndPage usageAndPage, RawInputDeviceFlags flags, IntPtr hWndTarget)
     : this(usageAndPage.UsagePage, usageAndPage.Usage, flags, hWndTarget)
 {
 }
 /// <summary>
 /// Create RawInputDevice, to use with RegisterRawInput
 /// </summary>
 /// <param name="hWnd">IntPtr with the window handle which handles the messages</param>
 /// <param name="usage">Generic Usage for the raw input device.</param>
 /// <param name="flags">RawInputDeviceFlags</param>
 /// <returns>RawInputDevice filled</returns>
 public static RawInputDevice CreateRawInputDevice(IntPtr hWnd, HidUsagesGeneric usage, RawInputDeviceFlags flags = RawInputDeviceFlags.InputSink)
 {
     return(new RawInputDevice
     {
         TargetHwnd = hWnd,
         Flags = flags,
         UsagePage = HidUsagePages.Generic,
         Usage = (ushort)usage
     });
 }
        /// <summary>
        /// Create RawInputDevice
        /// </summary>
        /// <param name="hWnd">IntPtr with the window handle which handles the messages</param>
        /// <param name="device">RawInputDevices</param>
        /// <param name="flags">RawInputDeviceFlags</param>
        /// <returns>RawInputDevice filled</returns>
        public static RawInputDevice CreateRawInputDevice(IntPtr hWnd, RawInputDevices device, RawInputDeviceFlags flags = RawInputDeviceFlags.InputSink)
        {
            switch (device)
            {
            case RawInputDevices.Pointer:
                return(CreateRawInputDevice(hWnd, HidUsagesGeneric.Pointer, flags));

            case RawInputDevices.Mouse:
                return(CreateRawInputDevice(hWnd, HidUsagesGeneric.Mouse, flags));

            case RawInputDevices.Joystick:
                return(CreateRawInputDevice(hWnd, HidUsagesGeneric.Joystick, flags));

            case RawInputDevices.GamePad:
                return(CreateRawInputDevice(hWnd, HidUsagesGeneric.Gamepad, flags));

            case RawInputDevices.Keyboard:
                return(CreateRawInputDevice(hWnd, HidUsagesGeneric.Keyboard, flags));

            case RawInputDevices.Keypad:
                return(CreateRawInputDevice(hWnd, HidUsagesGeneric.Keypad, flags));

            case RawInputDevices.SystemControl:
                return(CreateRawInputDevice(hWnd, HidUsagesGeneric.SystemControl, flags));

            case RawInputDevices.ConsumerAudioControl:
                return(CreateRawInputDevice(hWnd, HidUsagesConsumer.ConsumerControl, flags));

            default:
                throw new NotSupportedException($"Unknown RawInputDevices: {device}");
            }
        }
 /// <summary>
 /// Register the specified window to receive raw input, coming from the specified device
 /// </summary>
 /// <param name="hWnd">IntPtr for the window to receive the events</param>
 /// <param name="flags">RawInputDeviceFlags</param>
 /// <param name="devices">one or more RawInputDevices</param>
 public static void RegisterRawInput(IntPtr hWnd, RawInputDeviceFlags flags, params RawInputDevices[] devices)
 {
     RegisterRawInput(devices.Select(device => CreateRawInputDevice(hWnd, device, flags)).ToArray());
 }
Example #11
0
 public RawInputDevice(HIDUsageSim usage, RawInputDeviceFlags flags, HWND target)
 {
     UsagePage = HIDPage.Simulation;
     Usage = (short)usage;
     Flags = flags;
     Target = target;
 }
Example #12
0
 public RawInputDevice(HIDUsageGD usage, RawInputDeviceFlags flags, HWND target)
 {
     UsagePage = HIDPage.GenericDesktop;
     Usage = (short)usage;
     Flags = flags;
     Target = target;
 }
Example #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RawInputDevice"/> struct for simulation usage.
 /// </summary>
 /// <param name="usage">HID usage for the Simulation page.</param>
 /// <param name="flags">Specifies how to interpret the raw input data.</param>
 /// <param name="target">Handle to the target window.</param>
 public RawInputDevice(HidSimulationUsage usage, RawInputDeviceFlags flags, IntPtr target)
     : this((ushort)usage, flags, target, HidPage.Simulation)
 {
 }
Example #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RawInputDevice"/> struct for consumer usage.
 /// </summary>
 /// <param name="usage">HID usage for the Consumer page.</param>
 /// <param name="flags">Specifies how to interpret the raw input data.</param>
 /// <param name="target">Handle to the target window.</param>
 public RawInputDevice(HidConsumerUsage usage, RawInputDeviceFlags flags, IntPtr target)
     : this((ushort)usage, flags, target, HidPage.Consumer)
 {
 }
Example #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RawInputDevice"/> struct for generic desktop usage.
 /// </summary>
 /// <param name="usage">HID usage for the Generic Desktop page.</param>
 /// <param name="flags">Specifies how to interpret the raw input data.</param>
 /// <param name="target">Handle to the target window.</param>
 public RawInputDevice(HidGenericDesktopUsage usage, RawInputDeviceFlags flags, IntPtr target)
     : this((ushort)usage, flags, target, HidPage.GenericDesktop)
 {
 }