Win32RawInput.cs
author sl
Fri, 07 Nov 2014 22:12:22 +0100
changeset 10 17f8421146ba
parent 9 94850bfc12b5
child 15 2044181ade12
permissions -rw-r--r--
More cleanup and re-org.
     1 using System;
     2 using System.Runtime.InteropServices;
     3 
     4 namespace Win32
     5 {
     6 
     7     static partial class Function
     8     {
     9         [DllImport("User32.dll", SetLastError = true)]
    10 		public extern static bool RegisterRawInputDevices(RAWINPUTDEVICE[] pRawInputDevice, uint uiNumDevices, uint cbSize);
    11 
    12         [DllImport("User32.dll", SetLastError = true)]
    13 		public extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
    14 
    15    		[DllImport("User32.dll", SetLastError=true)]
    16 		public extern static int GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);
    17     }
    18 
    19     static partial class Const
    20     {
    21         /// <summary>
    22         /// GetRawInputDeviceInfo pData points to a string that contains the device name.
    23         /// </summary>
    24         public const uint RIDI_DEVICENAME = 0x20000007;
    25         /// <summary>
    26         /// GetRawInputDeviceInfo For this uiCommand only, the value in pcbSize is the character count (not the byte count).
    27         /// </summary>
    28         public const uint RIDI_DEVICEINFO = 0x2000000b;
    29         /// <summary>
    30         /// GetRawInputDeviceInfo pData points to an RID_DEVICE_INFO structure.
    31         /// </summary>
    32         public const uint RIDI_PREPARSEDDATA = 0x20000005;
    33 
    34 
    35         /// <summary>
    36         /// Data comes from a mouse.
    37         /// </summary>
    38         public const uint RIM_TYPEMOUSE = 0;
    39         /// <summary>
    40         /// Data comes from a keyboard.
    41         /// </summary>
    42         public const uint RIM_TYPEKEYBOARD = 1;
    43         /// <summary>
    44         /// Data comes from an HID that is not a keyboard or a mouse.
    45         /// </summary>
    46         public const uint RIM_TYPEHID = 2;
    47 
    48         public const int RID_INPUT = 0x10000003;
    49         public const int RID_HEADER = 0x10000005;
    50 
    51 
    52     }
    53 
    54 
    55     [StructLayout(LayoutKind.Sequential, Pack = 1)]
    56     internal struct RAWINPUTDEVICE
    57     {
    58         [MarshalAs(UnmanagedType.U2)]
    59         public ushort usUsagePage;
    60         [MarshalAs(UnmanagedType.U2)]
    61         public ushort usUsage;
    62         [MarshalAs(UnmanagedType.U4)]
    63         public int dwFlags;
    64         public IntPtr hwndTarget;
    65     }
    66 
    67 
    68     [StructLayout(LayoutKind.Sequential, Pack = 1)]
    69     internal struct RAWINPUTHEADER
    70     {
    71         [MarshalAs(UnmanagedType.U4)]
    72         public int dwType;
    73         [MarshalAs(UnmanagedType.U4)]
    74         public int dwSize;
    75         public IntPtr hDevice;
    76         [MarshalAs(UnmanagedType.U4)]
    77         public int wParam;
    78     }
    79 
    80 
    81     [StructLayout(LayoutKind.Sequential, Pack = 1)]
    82     internal struct RAWHID
    83     {
    84         [MarshalAs(UnmanagedType.U4)]
    85         public int dwSizeHid;
    86         [MarshalAs(UnmanagedType.U4)]
    87         public int dwCount;
    88         //
    89         //BYTE  bRawData[1];
    90     }
    91 
    92 
    93     [StructLayout(LayoutKind.Sequential, Pack = 1)]
    94     internal struct BUTTONSSTR
    95     {
    96         [MarshalAs(UnmanagedType.U2)]
    97         public ushort usButtonFlags;
    98         [MarshalAs(UnmanagedType.U2)]
    99         public ushort usButtonData;
   100     }
   101 
   102 
   103     [StructLayout(LayoutKind.Explicit, Pack = 1)]
   104     internal struct RAWMOUSE
   105     {
   106         [MarshalAs(UnmanagedType.U2)]
   107         [FieldOffset(0)]
   108         public ushort usFlags;
   109         [MarshalAs(UnmanagedType.U4)]
   110         [FieldOffset(4)]
   111         public uint ulButtons;
   112         [FieldOffset(4)]
   113         public BUTTONSSTR buttonsStr;
   114         [MarshalAs(UnmanagedType.U4)]
   115         [FieldOffset(8)]
   116         public uint ulRawButtons;
   117         [MarshalAs(UnmanagedType.U4)]
   118         [FieldOffset(12)]
   119         public int lLastX;
   120         [MarshalAs(UnmanagedType.U4)]
   121         [FieldOffset(16)]
   122         public int lLastY;
   123         [MarshalAs(UnmanagedType.U4)]
   124         [FieldOffset(20)]
   125         public uint ulExtraInformation;
   126     }
   127 
   128     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   129     internal struct RAWKEYBOARD
   130     {
   131         [MarshalAs(UnmanagedType.U2)]
   132         public ushort MakeCode;
   133         [MarshalAs(UnmanagedType.U2)]
   134         public ushort Flags;
   135         [MarshalAs(UnmanagedType.U2)]
   136         public ushort Reserved;
   137         [MarshalAs(UnmanagedType.U2)]
   138         public ushort VKey;
   139         [MarshalAs(UnmanagedType.U4)]
   140         public uint Message;
   141         [MarshalAs(UnmanagedType.U4)]
   142         public uint ExtraInformation;
   143     }
   144 
   145 
   146     [StructLayout(LayoutKind.Explicit, Pack = 1)]
   147     internal struct RAWINPUT
   148     {
   149         [FieldOffset(0)]
   150         public RAWINPUTHEADER header;
   151         [FieldOffset(16)]
   152         public RAWMOUSE mouse;
   153         [FieldOffset(16)]
   154         public RAWKEYBOARD keyboard;
   155         [FieldOffset(16)]
   156         public RAWHID hid;
   157     }
   158 
   159 
   160     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   161     internal struct RID_DEVICE_INFO_MOUSE
   162     {
   163         public uint dwId;
   164         public uint dwNumberOfButtons;
   165         public uint dwSampleRate;
   166         public bool fHasHorizontalWheel;
   167     }
   168 
   169 
   170     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   171     internal struct RID_DEVICE_INFO_KEYBOARD
   172     {
   173         public uint dwType;
   174         public uint dwSubType;
   175         public uint dwKeyboardMode;
   176         public uint dwNumberOfFunctionKeys;
   177         public uint dwNumberOfIndicators;
   178         public uint dwNumberOfKeysTotal;
   179     }
   180 
   181     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   182     internal struct RID_DEVICE_INFO_HID
   183     {
   184         public uint dwVendorId;
   185         public uint dwProductId;
   186         public uint dwVersionNumber;
   187         public ushort usUsagePage;
   188         public ushort usUsage;
   189     }
   190 
   191     [StructLayout(LayoutKind.Explicit, Pack = 1)]
   192     internal struct RID_DEVICE_INFO
   193     {
   194         [FieldOffset(0)]
   195         public uint cbSize;
   196         [FieldOffset(4)]
   197         public uint dwType;
   198         [FieldOffset(8)]
   199         public RID_DEVICE_INFO_MOUSE mouse;
   200         [FieldOffset(8)]
   201         public RID_DEVICE_INFO_KEYBOARD keyboard;
   202         [FieldOffset(8)]
   203         public RID_DEVICE_INFO_HID hid;
   204     }
   205 
   206 
   207 }