RegisterRawInputDevices (user32)
Last changed: -208.53.98.130

.
Summary
Registers a device to send its raw input data.

C# Signature:

    /// <summary>
    /// Function to register a raw input device.
    /// </summary>
    /// <param name="pRawInputDevices">Array of raw input devices.</param>
    /// <param name="uiNumDevices">Number of devices.</param>
    /// <param name="cbSize">Size of the RAWINPUTDEVICE structure.</param>
    /// <returns>TRUE if successful, FALSE if not.</returns>
    [DllImport("user32.dll")]
    public static extern bool RegisterRawInputDevices([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] RAWINPUTDEVICE[] pRawInputDevices, int uiNumDevices, int cbSize);

VB Signature:

Declare Function RegisterRawInputDevices Lib "user32.dll" (TODO) As TODO

User-Defined Types:

Enumerations:

    /// <summary>
    /// Enumeration containing flags for a raw input device.
    /// </summary>
    [Flags()]
    public enum RawInputDeviceFlags
    {
        /// <summary>No flags.</summary>
        None = 0,
        /// <summary>If set, this removes the top level collection from the inclusion list. This tells the operating system to stop reading from a device which matches the top level collection.</summary>
        Remove = 0x00000001,
        /// <summary>If set, this specifies the top level collections to exclude when reading a complete usage page. This flag only affects a TLC whose usage page is already specified with PageOnly.</summary>
        Exclude = 0x00000010,
        /// <summary>If set, this specifies all devices whose top level collection is from the specified usUsagePage. Note that Usage must be zero. To exclude a particular top level collection, use Exclude.</summary>
        PageOnly = 0x00000020,
        /// <summary>If set, this prevents any devices specified by UsagePage or Usage from generating legacy messages. This is only for the mouse and keyboard.</summary>
        NoLegacy = 0x00000030,
        /// <summary>If set, this enables the caller to receive the input even when the caller is not in the foreground. Note that WindowHandle must be specified.</summary>
        InputSink = 0x00000100,
        /// <summary>If set, the mouse button click does not activate the other window.</summary>
        CaptureMouse = 0x00000200,
        /// <summary>If set, the application-defined keyboard device hotkeys are not handled. However, the system hotkeys; for example, ALT+TAB and CTRL+ALT+DEL, are still handled. By default, all keyboard hotkeys are handled. NoHotKeys can be specified even if NoLegacy is not specified and WindowHandle is NULL.</summary>
        NoHotKeys = 0x00000200,
        /// <summary>If set, application keys are handled.  NoLegacy must be specified.  Keyboard only.</summary>
        AppKeys = 0x00000400
    }

Value types:

    /// <summary>
    /// Value type for raw input devices.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct RAWINPUTDEVICE
    {
        /// <summary>Top level collection Usage page for the raw input device.</summary>
        public short UsagePage;
        /// <summary>Top level collection Usage for the raw input device. </summary>
        public short Usage;
        /// <summary>Mode flag that specifies how to interpret the information provided by UsagePage and Usage.</summary>
        public RawInputDeviceFlags Flags;
        /// <summary>Handle to the target device. If NULL, it follows the keyboard focus.</summary>
        public IntPtr WindowHandle;
    }

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    private void RegisterMouse()
    {
        RAWINPUTDEVICE device;

        device.WindowHandle = this.Handle;
        device.UsagePage = 0x01;
        device.Usage = 0x02;
        device.Flags = RawInputDeviceFlags.InputSink;

        Win32API.RegisterRawInputDevices(device);
    }

    /// <summary>
    /// Function to register a raw input device.
    /// </summary>
    /// <param name="device">Device information.</param>
    /// <returns>TRUE if successful, FALSE if not.</returns>
    public static bool RegisterRawInputDevices(RAWINPUTDEVICE device)
    {
        RAWINPUTDEVICE[] devices = new RAWINPUTDEVICE[1];        // Raw input devices.

        devices[0] = device;
        return RegisterRawInputDevices(devices, 1, Marshal.SizeOf(typeof(RAWINPUTDEVICE)));
    }

Documentation