Cast fix and moving Win32 stuff in a dedicated file.
2 using System.Runtime.InteropServices;
7 static partial class Function
9 [DllImport("User32.dll", SetLastError = true)]
10 public extern static bool RegisterRawInputDevices(RAWINPUTDEVICE[] pRawInputDevice, uint uiNumDevices, uint cbSize);
12 [DllImport("User32.dll", SetLastError = true)]
13 public extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
15 [DllImport("User32.dll", SetLastError=true)]
16 public extern static int GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);
19 static partial class Const
22 /// GetRawInputDeviceInfo pData points to a string that contains the device name.
24 public const uint RIDI_DEVICENAME = 0x20000007;
26 /// GetRawInputDeviceInfo For this uiCommand only, the value in pcbSize is the character count (not the byte count).
28 public const uint RIDI_DEVICEINFO = 0x2000000b;
30 /// GetRawInputDeviceInfo pData points to an RID_DEVICE_INFO structure.
32 public const uint RIDI_PREPARSEDDATA = 0x20000005;
36 /// Data comes from a mouse.
38 public const uint RIM_TYPEMOUSE = 0;
40 /// Data comes from a keyboard.
42 public const uint RIM_TYPEKEYBOARD = 1;
44 /// Data comes from an HID that is not a keyboard or a mouse.
46 public const uint RIM_TYPEHID = 2;
51 [StructLayout(LayoutKind.Sequential, Pack = 1)]
52 internal struct RAWINPUTDEVICE
54 [MarshalAs(UnmanagedType.U2)]
55 public ushort usUsagePage;
56 [MarshalAs(UnmanagedType.U2)]
57 public ushort usUsage;
58 [MarshalAs(UnmanagedType.U4)]
60 public IntPtr hwndTarget;
64 [StructLayout(LayoutKind.Sequential, Pack = 1)]
65 internal struct RAWINPUTHEADER
67 [MarshalAs(UnmanagedType.U4)]
69 [MarshalAs(UnmanagedType.U4)]
71 public IntPtr hDevice;
72 [MarshalAs(UnmanagedType.U4)]
77 [StructLayout(LayoutKind.Sequential, Pack = 1)]
78 internal struct RAWHID
80 [MarshalAs(UnmanagedType.U4)]
82 [MarshalAs(UnmanagedType.U4)]
89 [StructLayout(LayoutKind.Sequential, Pack = 1)]
90 internal struct BUTTONSSTR
92 [MarshalAs(UnmanagedType.U2)]
93 public ushort usButtonFlags;
94 [MarshalAs(UnmanagedType.U2)]
95 public ushort usButtonData;
99 [StructLayout(LayoutKind.Explicit, Pack = 1)]
100 internal struct RAWMOUSE
102 [MarshalAs(UnmanagedType.U2)]
104 public ushort usFlags;
105 [MarshalAs(UnmanagedType.U4)]
107 public uint ulButtons;
109 public BUTTONSSTR buttonsStr;
110 [MarshalAs(UnmanagedType.U4)]
112 public uint ulRawButtons;
113 [MarshalAs(UnmanagedType.U4)]
116 [MarshalAs(UnmanagedType.U4)]
119 [MarshalAs(UnmanagedType.U4)]
121 public uint ulExtraInformation;
124 [StructLayout(LayoutKind.Sequential, Pack = 1)]
125 internal struct RAWKEYBOARD
127 [MarshalAs(UnmanagedType.U2)]
128 public ushort MakeCode;
129 [MarshalAs(UnmanagedType.U2)]
131 [MarshalAs(UnmanagedType.U2)]
132 public ushort Reserved;
133 [MarshalAs(UnmanagedType.U2)]
135 [MarshalAs(UnmanagedType.U4)]
137 [MarshalAs(UnmanagedType.U4)]
138 public uint ExtraInformation;
142 [StructLayout(LayoutKind.Explicit, Pack = 1)]
143 internal struct RAWINPUT
146 public RAWINPUTHEADER header;
148 public RAWMOUSE mouse;
150 public RAWKEYBOARD keyboard;
156 [StructLayout(LayoutKind.Sequential, Pack = 1)]
157 internal struct RID_DEVICE_INFO_MOUSE
160 public uint dwNumberOfButtons;
161 public uint dwSampleRate;
162 public bool fHasHorizontalWheel;
166 [StructLayout(LayoutKind.Sequential, Pack = 1)]
167 internal struct RID_DEVICE_INFO_KEYBOARD
170 public uint dwSubType;
171 public uint dwKeyboardMode;
172 public uint dwNumberOfFunctionKeys;
173 public uint dwNumberOfIndicators;
174 public uint dwNumberOfKeysTotal;
177 [StructLayout(LayoutKind.Sequential, Pack = 1)]
178 internal struct RID_DEVICE_INFO_HID
180 public uint dwVendorId;
181 public uint dwProductId;
182 public uint dwVersionNumber;
183 public ushort usUsagePage;
184 public ushort usUsage;
187 [StructLayout(LayoutKind.Explicit, Pack = 1)]
188 internal struct RID_DEVICE_INFO
195 public RID_DEVICE_INFO_MOUSE mouse;
197 public RID_DEVICE_INFO_KEYBOARD keyboard;
199 public RID_DEVICE_INFO_HID hid;