Win32Hid: Fixing boolean bug and improving value caps union.
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);
20 static partial class Macro
23 /// Retrieves the input code from wParam in WM_INPUT.
24 /// See RIM_INPUT and RIM_INPUTSINK.
26 /// <param name="wParam"></param>
27 /// <returns></returns>
28 public static int GET_RAWINPUT_CODE_WPARAM(IntPtr wParam)
30 return (wParam.ToInt32() & 0xff);
33 public static int GET_DEVICE_LPARAM(IntPtr lParam)
35 return ((ushort)(HIWORD(lParam.ToInt32()) & Const.FAPPCOMMAND_MASK));
38 public static int HIWORD(int val)
40 return ((val >> 16) & 0xffff);
44 //#define HIWORD(l) ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff))
45 //#define LOWORD(l) ((WORD)(((DWORD_PTR)(l)) & 0xffff))
46 //#define LOBYTE(w) ((BYTE)(((DWORD_PTR)(w)) & 0xff))
47 //#define HIBYTE(w) ((BYTE)((((DWORD_PTR)(w)) >> 8) & 0xff))
49 //#define GET_APPCOMMAND_LPARAM(lParam) ((short)(HIWORD(lParam) & ~FAPPCOMMAND_MASK))
50 //#define GET_DEVICE_LPARAM(lParam) ((WORD)(HIWORD(lParam) & FAPPCOMMAND_MASK))
51 //#define GET_MOUSEORKEY_LPARAM GET_DEVICE_LPARAM
52 //#define GET_FLAGS_LPARAM(lParam) (LOWORD(lParam))
53 //#define GET_KEYSTATE_LPARAM(lParam) GET_FLAGS_LPARAM(lParam)
59 static partial class Const
64 public const int WM_KEYDOWN = 0x0100;
65 public const int WM_INPUT = 0x00FF;
68 /// GetRawInputDeviceInfo pData points to a string that contains the device name.
70 public const uint RIDI_DEVICENAME = 0x20000007;
72 /// GetRawInputDeviceInfo For this uiCommand only, the value in pcbSize is the character count (not the byte count).
74 public const uint RIDI_DEVICEINFO = 0x2000000b;
76 /// GetRawInputDeviceInfo pData points to an RID_DEVICE_INFO structure.
78 public const uint RIDI_PREPARSEDDATA = 0x20000005;
82 /// Data comes from a mouse.
84 public const uint RIM_TYPEMOUSE = 0;
86 /// Data comes from a keyboard.
88 public const uint RIM_TYPEKEYBOARD = 1;
90 /// Data comes from an HID that is not a keyboard or a mouse.
92 public const uint RIM_TYPEHID = 2;
94 public const int RID_INPUT = 0x10000003;
95 public const int RID_HEADER = 0x10000005;
98 /// Possible value taken by wParam for WM_INPUT.
100 /// Input occurred while the application was in the foreground. The application must call DefWindowProc so the system can perform cleanup.
102 public const int RIM_INPUT = 0;
104 /// Possible value taken by wParam for WM_INPUT.
106 /// Input occurred while the application was not in the foreground. The application must call DefWindowProc so the system can perform the cleanup.
108 public const int RIM_INPUTSINK = 1;
111 /// If set, the application command keys are handled. RIDEV_APPKEYS can be specified only if RIDEV_NOLEGACY is specified for a keyboard device.
113 public const uint RIDEV_APPKEYS = 0x00000400;
116 /// If set, the mouse button click does not activate the other window.
118 public const uint RIDEV_CAPTUREMOUSE = 0x00000200;
121 /// If set, this enables the caller to receive WM_INPUT_DEVICE_CHANGE notifications for device arrival and device removal.
122 /// Windows XP: This flag is not supported until Windows Vista
124 public const uint RIDEV_DEVNOTIFY = 0x00002000;
127 /// 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 RIDEV_PAGEONLY.
129 public const uint RIDEV_EXCLUDE = 0x00000010;
132 /// If set, this enables the caller to receive input in the background only if the foreground application does not process it. In other words, if the foreground application is not registered for raw input, then the background application that is registered will receive the input.
133 /// Windows XP: This flag is not supported until Windows Vista
135 public const uint RIDEV_EXINPUTSINK = 0x00001000;
138 /// If set, this enables the caller to receive the input even when the caller is not in the foreground. Note that hwndTarget must be specified.
140 public const uint RIDEV_INPUTSINK = 0x00000100;
143 /// 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. RIDEV_NOHOTKEYS can be specified even if RIDEV_NOLEGACY is not specified and hwndTarget is NULL.
145 public const uint RIDEV_NOHOTKEYS = 0x00000200;
148 /// If set, this prevents any devices specified by usUsagePage or usUsage from generating legacy messages. This is only for the mouse and keyboard. See Remarks.
150 public const uint RIDEV_NOLEGACY = 0x00000030;
153 /// If set, this specifies all devices whose top level collection is from the specified usUsagePage. Note that usUsage must be zero. To exclude a particular top level collection, use RIDEV_EXCLUDE.
155 public const uint RIDEV_PAGEONLY = 0x00000020;
158 /// 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.
160 public const uint RIDEV_REMOVE = 0x00000001;
162 public const int APPCOMMAND_BROWSER_BACKWARD = 1;
163 public const int APPCOMMAND_VOLUME_MUTE = 8;
164 public const int APPCOMMAND_VOLUME_DOWN = 9;
165 public const int APPCOMMAND_VOLUME_UP = 10;
166 public const int APPCOMMAND_MEDIA_NEXTTRACK = 11;
167 public const int APPCOMMAND_MEDIA_PREVIOUSTRACK = 12;
168 public const int APPCOMMAND_MEDIA_STOP = 13;
169 public const int APPCOMMAND_MEDIA_PLAY_PAUSE = 14;
170 public const int APPCOMMAND_MEDIA_PLAY = 46;
171 public const int APPCOMMAND_MEDIA_PAUSE = 47;
172 public const int APPCOMMAND_MEDIA_RECORD = 48;
173 public const int APPCOMMAND_MEDIA_FAST_FORWARD = 49;
174 public const int APPCOMMAND_MEDIA_REWIND = 50;
175 public const int APPCOMMAND_MEDIA_CHANNEL_UP = 51;
176 public const int APPCOMMAND_MEDIA_CHANNEL_DOWN = 52;
178 public const int FAPPCOMMAND_MASK = 0xF000;
179 public const int FAPPCOMMAND_MOUSE = 0x8000;
180 public const int FAPPCOMMAND_KEY = 0;
181 public const int FAPPCOMMAND_OEM = 0x1000;
185 [StructLayout(LayoutKind.Sequential, Pack = 1)]
186 public struct RAWINPUTDEVICELIST
188 public IntPtr hDevice;
192 [StructLayout(LayoutKind.Sequential, Pack = 1)]
193 public struct RAWINPUTDEVICE
195 [MarshalAs(UnmanagedType.U2)]
196 public ushort usUsagePage;
197 [MarshalAs(UnmanagedType.U2)]
198 public ushort usUsage;
199 [MarshalAs(UnmanagedType.U4)]
201 public IntPtr hwndTarget;
205 [StructLayout(LayoutKind.Sequential, Pack = 1)]
206 public struct RAWINPUTHEADER
208 [MarshalAs(UnmanagedType.U4)]
210 [MarshalAs(UnmanagedType.U4)]
212 public IntPtr hDevice;
213 [MarshalAs(UnmanagedType.U4)]
218 [StructLayout(LayoutKind.Sequential, Pack = 1)]
221 [MarshalAs(UnmanagedType.U4)]
222 public uint dwSizeHid;
223 [MarshalAs(UnmanagedType.U4)]
230 [StructLayout(LayoutKind.Sequential, Pack = 1)]
231 public struct BUTTONSSTR
233 [MarshalAs(UnmanagedType.U2)]
234 public ushort usButtonFlags;
235 [MarshalAs(UnmanagedType.U2)]
236 public ushort usButtonData;
240 [StructLayout(LayoutKind.Explicit, Pack = 1)]
241 public struct RAWMOUSE
243 [MarshalAs(UnmanagedType.U2)]
245 public ushort usFlags;
246 [MarshalAs(UnmanagedType.U4)]
248 public uint ulButtons;
250 public BUTTONSSTR buttonsStr;
251 [MarshalAs(UnmanagedType.U4)]
253 public uint ulRawButtons;
254 [MarshalAs(UnmanagedType.U4)]
257 [MarshalAs(UnmanagedType.U4)]
260 [MarshalAs(UnmanagedType.U4)]
262 public uint ulExtraInformation;
265 [StructLayout(LayoutKind.Sequential, Pack = 1)]
266 public struct RAWKEYBOARD
268 [MarshalAs(UnmanagedType.U2)]
269 public ushort MakeCode;
270 [MarshalAs(UnmanagedType.U2)]
272 [MarshalAs(UnmanagedType.U2)]
273 public ushort Reserved;
274 [MarshalAs(UnmanagedType.U2)]
276 [MarshalAs(UnmanagedType.U4)]
278 [MarshalAs(UnmanagedType.U4)]
279 public uint ExtraInformation;
283 [StructLayout(LayoutKind.Explicit, Pack = 1)]
284 public struct RAWINPUT
287 public RAWINPUTHEADER header;
289 public RAWMOUSE mouse;
291 public RAWKEYBOARD keyboard;
297 [StructLayout(LayoutKind.Sequential, Pack = 1)]
298 public struct RID_DEVICE_INFO_MOUSE
301 public uint dwNumberOfButtons;
302 public uint dwSampleRate;
303 public bool fHasHorizontalWheel;
307 [StructLayout(LayoutKind.Sequential, Pack = 1)]
308 public struct RID_DEVICE_INFO_KEYBOARD
311 public uint dwSubType;
312 public uint dwKeyboardMode;
313 public uint dwNumberOfFunctionKeys;
314 public uint dwNumberOfIndicators;
315 public uint dwNumberOfKeysTotal;
318 [StructLayout(LayoutKind.Sequential, Pack = 1)]
319 public struct RID_DEVICE_INFO_HID
321 public uint dwVendorId;
322 public uint dwProductId;
323 public uint dwVersionNumber;
324 public ushort usUsagePage;
325 public ushort usUsage;
328 [StructLayout(LayoutKind.Explicit, Pack = 1)]
329 public struct RID_DEVICE_INFO
336 public RID_DEVICE_INFO_MOUSE mouse;
338 public RID_DEVICE_INFO_KEYBOARD keyboard;
340 public RID_DEVICE_INFO_HID hid;