Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetRawInputData (user32)
 
.
Summary
Function to read raw data from an input device such as a keyboard, mouse or other HID.

C# Signature:

    /// <summary>
    /// Function to retrieve raw input data.
    /// </summary>
    /// <param name="hRawInput">Handle to the raw input.</param>
    /// <param name="uiCommand">Command to issue when retrieving data.</param>
    /// <param name="pData">Raw input data.</param>
    /// <param name="pcbSize">Number of bytes in the array.</param>
    /// <param name="cbSizeHeader">Size of the header.</param>
    /// <returns>0 if successful if pData is null, otherwise number of bytes if pData is not null.</returns>
    [DllImport("user32.dll")]
    public static extern int GetRawInputData(IntPtr hRawInput, RawInputCommand uiCommand, out RAWINPUT pData, ref int pcbSize, int cbSizeHeader);

or

    /// <summary>
    /// Function to retrieve raw input data.
    /// </summary>
    /// <param name="hRawInput">Handle to the raw input.</param>
    /// <param name="uiCommand">Command to issue when retrieving data.</param>
    /// <param name="pData">Raw input data.</param>
    /// <param name="pcbSize">Number of bytes in the array.</param>
    /// <param name="cbSizeHeader">Size of the header.</param>
    /// <returns>0 if successful if pData is null, otherwise number of bytes if pData is not null.</returns>
    [DllImport("user32.dll")]
    public static extern int GetRawInputData(IntPtr hRawInput, RawInputCommand uiCommand, byte[] pData, ref int pcbSize, int cbSizeHeader);

VB.NET Signature:

    <DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="GetRawInputData", SetLastError:=True)>
    Function GetRawInputData(hRawInput As IntPtr,
                    uiCommand As UInteger,
                    ByRef pData As IntPtr,
                    ByRef pcbSize As IntPtr,
                    cbSizeHeader As UInteger)
    End Function

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

The VirtualKeys (they're just mapped to the VK_* constants) and WindowsMessages enumeration have been omitted as it's quite lengthy.

RAWINPUT is incompatible with x64. See RAWINPUTHEADER.Device and RAWINPUTHEADER.wParam.

Tips & Tricks:

Please add some!

Sample Code:

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == (int)WindowMessages.RawInput)  // WindowMessages.RawInput = 0x00FF (WM_INPUT)
        {
            RAWINPUT input = new RAWINPUT();
            int outSize = 0;
            int size = Marshal.SizeOf(typeof(RAWINPUT));

            outSize = Win32API.GetRawInputData(m.LParam, RawInputCommand.Input, out input, ref size, Marshal.SizeOf(typeof(RAWINPUTHEADER)));
            if (outSize != -1)
            {
                if (input.Header.Type == RawInputType.Mouse)
                {
                    p.X += input.Mouse.LastX;
                    p.Y += input.Mouse.LastY;
                    label1.Text = "Mouse: " + p.X.ToString() + "x" + p.Y.ToString() + " " + input.Mouse.ButtonFlags.ToString();
                }
            }
        }
        base.WndProc(ref m);
    }

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions