Win32RawInput.cs
author sl
Fri, 07 Nov 2014 20:49:51 +0100
changeset 9 94850bfc12b5
child 10 17f8421146ba
permissions -rw-r--r--
Cast fix and moving Win32 stuff in a dedicated file.
     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     }
    49 
    50 
    51     [StructLayout(LayoutKind.Sequential, Pack = 1)]
    52     internal struct RAWINPUTDEVICE
    53     {
    54         [MarshalAs(UnmanagedType.U2)]
    55         public ushort usUsagePage;
    56         [MarshalAs(UnmanagedType.U2)]
    57         public ushort usUsage;
    58         [MarshalAs(UnmanagedType.U4)]
    59         public int dwFlags;
    60         public IntPtr hwndTarget;
    61     }
    62 
    63 
    64     [StructLayout(LayoutKind.Sequential, Pack = 1)]
    65     internal struct RAWINPUTHEADER
    66     {
    67         [MarshalAs(UnmanagedType.U4)]
    68         public int dwType;
    69         [MarshalAs(UnmanagedType.U4)]
    70         public int dwSize;
    71         public IntPtr hDevice;
    72         [MarshalAs(UnmanagedType.U4)]
    73         public int wParam;
    74     }
    75 
    76 
    77     [StructLayout(LayoutKind.Sequential, Pack = 1)]
    78     internal struct RAWHID
    79     {
    80         [MarshalAs(UnmanagedType.U4)]
    81         public int dwSizeHid;
    82         [MarshalAs(UnmanagedType.U4)]
    83         public int dwCount;
    84         //
    85         //BYTE  bRawData[1];
    86     }
    87 
    88 
    89     [StructLayout(LayoutKind.Sequential, Pack = 1)]
    90     internal struct BUTTONSSTR
    91     {
    92         [MarshalAs(UnmanagedType.U2)]
    93         public ushort usButtonFlags;
    94         [MarshalAs(UnmanagedType.U2)]
    95         public ushort usButtonData;
    96     }
    97 
    98 
    99     [StructLayout(LayoutKind.Explicit, Pack = 1)]
   100     internal struct RAWMOUSE
   101     {
   102         [MarshalAs(UnmanagedType.U2)]
   103         [FieldOffset(0)]
   104         public ushort usFlags;
   105         [MarshalAs(UnmanagedType.U4)]
   106         [FieldOffset(4)]
   107         public uint ulButtons;
   108         [FieldOffset(4)]
   109         public BUTTONSSTR buttonsStr;
   110         [MarshalAs(UnmanagedType.U4)]
   111         [FieldOffset(8)]
   112         public uint ulRawButtons;
   113         [MarshalAs(UnmanagedType.U4)]
   114         [FieldOffset(12)]
   115         public int lLastX;
   116         [MarshalAs(UnmanagedType.U4)]
   117         [FieldOffset(16)]
   118         public int lLastY;
   119         [MarshalAs(UnmanagedType.U4)]
   120         [FieldOffset(20)]
   121         public uint ulExtraInformation;
   122     }
   123 
   124     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   125     internal struct RAWKEYBOARD
   126     {
   127         [MarshalAs(UnmanagedType.U2)]
   128         public ushort MakeCode;
   129         [MarshalAs(UnmanagedType.U2)]
   130         public ushort Flags;
   131         [MarshalAs(UnmanagedType.U2)]
   132         public ushort Reserved;
   133         [MarshalAs(UnmanagedType.U2)]
   134         public ushort VKey;
   135         [MarshalAs(UnmanagedType.U4)]
   136         public uint Message;
   137         [MarshalAs(UnmanagedType.U4)]
   138         public uint ExtraInformation;
   139     }
   140 
   141 
   142     [StructLayout(LayoutKind.Explicit, Pack = 1)]
   143     internal struct RAWINPUT
   144     {
   145         [FieldOffset(0)]
   146         public RAWINPUTHEADER header;
   147         [FieldOffset(16)]
   148         public RAWMOUSE mouse;
   149         [FieldOffset(16)]
   150         public RAWKEYBOARD keyboard;
   151         [FieldOffset(16)]
   152         public RAWHID hid;
   153     }
   154 
   155 
   156     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   157     internal struct RID_DEVICE_INFO_MOUSE
   158     {
   159         public uint dwId;
   160         public uint dwNumberOfButtons;
   161         public uint dwSampleRate;
   162         public bool fHasHorizontalWheel;
   163     }
   164 
   165 
   166     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   167     internal struct RID_DEVICE_INFO_KEYBOARD
   168     {
   169         public uint dwType;
   170         public uint dwSubType;
   171         public uint dwKeyboardMode;
   172         public uint dwNumberOfFunctionKeys;
   173         public uint dwNumberOfIndicators;
   174         public uint dwNumberOfKeysTotal;
   175     }
   176 
   177     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   178     internal struct RID_DEVICE_INFO_HID
   179     {
   180         public uint dwVendorId;
   181         public uint dwProductId;
   182         public uint dwVersionNumber;
   183         public ushort usUsagePage;
   184         public ushort usUsage;
   185     }
   186 
   187     [StructLayout(LayoutKind.Explicit, Pack = 1)]
   188     internal struct RID_DEVICE_INFO
   189     {
   190         [FieldOffset(0)]
   191         public uint cbSize;
   192         [FieldOffset(4)]
   193         public uint dwType;
   194         [FieldOffset(8)]
   195         public RID_DEVICE_INFO_MOUSE mouse;
   196         [FieldOffset(8)]
   197         public RID_DEVICE_INFO_KEYBOARD keyboard;
   198         [FieldOffset(8)]
   199         public RID_DEVICE_INFO_HID hid;
   200     }
   201 
   202 
   203 }