Renaming demo application to HID Demo.
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, RawInputDeviceInfoType uiCommand, IntPtr pData, ref uint pcbSize);
18 [DllImport("user32.dll", SetLastError = true)]
19 public static extern int GetRawInputDeviceList(
20 [In, Out] RAWINPUTDEVICELIST[] InputdeviceList,
21 [In, Out] ref uint puiNumDevices,
27 static partial class Macro
30 /// Retrieves the input code from wParam in WM_INPUT.
31 /// See RIM_INPUT and RIM_INPUTSINK.
33 /// <param name="wParam"></param>
34 /// <returns></returns>
35 public static int GET_RAWINPUT_CODE_WPARAM(IntPtr wParam)
37 return (wParam.ToInt32() & 0xff);
40 public static int GET_DEVICE_LPARAM(IntPtr lParam)
42 return ((ushort)(HIWORD(lParam.ToInt32()) & Const.FAPPCOMMAND_MASK));
45 public static int HIWORD(int val)
47 return ((val >> 16) & 0xffff);
51 //#define HIWORD(l) ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff))
52 //#define LOWORD(l) ((WORD)(((DWORD_PTR)(l)) & 0xffff))
53 //#define LOBYTE(w) ((BYTE)(((DWORD_PTR)(w)) & 0xff))
54 //#define HIBYTE(w) ((BYTE)((((DWORD_PTR)(w)) >> 8) & 0xff))
56 //#define GET_APPCOMMAND_LPARAM(lParam) ((short)(HIWORD(lParam) & ~FAPPCOMMAND_MASK))
57 //#define GET_DEVICE_LPARAM(lParam) ((WORD)(HIWORD(lParam) & FAPPCOMMAND_MASK))
58 //#define GET_MOUSEORKEY_LPARAM GET_DEVICE_LPARAM
59 //#define GET_FLAGS_LPARAM(lParam) (LOWORD(lParam))
60 //#define GET_KEYSTATE_LPARAM(lParam) GET_FLAGS_LPARAM(lParam)
66 static partial class Const
71 public const int WM_KEYDOWN = 0x0100;
72 public const int WM_INPUT = 0x00FF;
76 public const int RID_INPUT = 0x10000003;
77 public const int RID_HEADER = 0x10000005;
80 /// Possible value taken by wParam for WM_INPUT.
82 /// Input occurred while the application was in the foreground. The application must call DefWindowProc so the system can perform cleanup.
84 public const int RIM_INPUT = 0;
86 /// Possible value taken by wParam for WM_INPUT.
88 /// Input occurred while the application was not in the foreground. The application must call DefWindowProc so the system can perform the cleanup.
90 public const int RIM_INPUTSINK = 1;
93 /// If set, the application command keys are handled. RIDEV_APPKEYS can be specified only if RIDEV_NOLEGACY is specified for a keyboard device.
95 public const uint RIDEV_APPKEYS = 0x00000400;
98 /// If set, the mouse button click does not activate the other window.
100 public const uint RIDEV_CAPTUREMOUSE = 0x00000200;
103 /// If set, this enables the caller to receive WM_INPUT_DEVICE_CHANGE notifications for device arrival and device removal.
104 /// Windows XP: This flag is not supported until Windows Vista
106 public const uint RIDEV_DEVNOTIFY = 0x00002000;
109 /// 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.
111 public const uint RIDEV_EXCLUDE = 0x00000010;
114 /// 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.
115 /// Windows XP: This flag is not supported until Windows Vista
117 public const uint RIDEV_EXINPUTSINK = 0x00001000;
120 /// 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.
122 public const uint RIDEV_INPUTSINK = 0x00000100;
125 /// 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.
127 public const uint RIDEV_NOHOTKEYS = 0x00000200;
130 /// 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.
132 public const uint RIDEV_NOLEGACY = 0x00000030;
135 /// 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.
137 public const uint RIDEV_PAGEONLY = 0x00000020;
140 /// 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.
142 public const uint RIDEV_REMOVE = 0x00000001;
144 public const int APPCOMMAND_BROWSER_BACKWARD = 1;
145 public const int APPCOMMAND_VOLUME_MUTE = 8;
146 public const int APPCOMMAND_VOLUME_DOWN = 9;
147 public const int APPCOMMAND_VOLUME_UP = 10;
148 public const int APPCOMMAND_MEDIA_NEXTTRACK = 11;
149 public const int APPCOMMAND_MEDIA_PREVIOUSTRACK = 12;
150 public const int APPCOMMAND_MEDIA_STOP = 13;
151 public const int APPCOMMAND_MEDIA_PLAY_PAUSE = 14;
152 public const int APPCOMMAND_MEDIA_PLAY = 46;
153 public const int APPCOMMAND_MEDIA_PAUSE = 47;
154 public const int APPCOMMAND_MEDIA_RECORD = 48;
155 public const int APPCOMMAND_MEDIA_FAST_FORWARD = 49;
156 public const int APPCOMMAND_MEDIA_REWIND = 50;
157 public const int APPCOMMAND_MEDIA_CHANNEL_UP = 51;
158 public const int APPCOMMAND_MEDIA_CHANNEL_DOWN = 52;
160 public const int FAPPCOMMAND_MASK = 0xF000;
161 public const int FAPPCOMMAND_MOUSE = 0x8000;
162 public const int FAPPCOMMAND_KEY = 0;
163 public const int FAPPCOMMAND_OEM = 0x1000;
167 /// Introduced this enum for consistency and easy of use.
168 /// Naming of the Win32 constants were preserved.
170 public enum RawInputDeviceType : uint
173 /// Data comes from a mouse.
177 /// Data comes from a keyboard.
179 RIM_TYPEKEYBOARD = 1,
181 /// Data comes from an HID that is not a keyboard or a mouse.
187 /// Introduced this enum for consistency and easy of use.
188 /// Naming of the Win32 constants were preserved.
190 public enum RawInputDeviceInfoType : uint
193 /// GetRawInputDeviceInfo pData points to a string that contains the device name.
195 RIDI_DEVICENAME = 0x20000007,
197 /// GetRawInputDeviceInfo For this uiCommand only, the value in pcbSize is the character count (not the byte count).
199 RIDI_DEVICEINFO = 0x2000000b,
201 /// GetRawInputDeviceInfo pData points to an RID_DEVICE_INFO structure.
203 RIDI_PREPARSEDDATA = 0x20000005
207 [StructLayout(LayoutKind.Sequential, Pack = 1)]
208 public struct RAWINPUTDEVICELIST
210 public IntPtr hDevice;
211 public RawInputDeviceType dwType;
214 [StructLayout(LayoutKind.Sequential, Pack = 1)]
215 public struct RAWINPUTDEVICE
217 [MarshalAs(UnmanagedType.U2)]
218 public ushort usUsagePage;
219 [MarshalAs(UnmanagedType.U2)]
220 public ushort usUsage;
221 [MarshalAs(UnmanagedType.U4)]
223 public IntPtr hwndTarget;
227 [StructLayout(LayoutKind.Sequential, Pack = 1)]
228 public struct RAWINPUTHEADER
230 [MarshalAs(UnmanagedType.U4)]
231 public RawInputDeviceType dwType;
232 [MarshalAs(UnmanagedType.U4)]
234 public IntPtr hDevice;
235 [MarshalAs(UnmanagedType.U4)]
240 [StructLayout(LayoutKind.Sequential, Pack = 1)]
243 [MarshalAs(UnmanagedType.U4)]
244 public uint dwSizeHid;
245 [MarshalAs(UnmanagedType.U4)]
252 [StructLayout(LayoutKind.Sequential, Pack = 1)]
253 public struct BUTTONSSTR
255 [MarshalAs(UnmanagedType.U2)]
256 public ushort usButtonFlags;
257 [MarshalAs(UnmanagedType.U2)]
258 public ushort usButtonData;
262 [StructLayout(LayoutKind.Explicit, Pack = 1)]
263 public struct RAWMOUSE
265 [MarshalAs(UnmanagedType.U2)]
267 public ushort usFlags;
268 [MarshalAs(UnmanagedType.U4)]
270 public uint ulButtons;
272 public BUTTONSSTR buttonsStr;
273 [MarshalAs(UnmanagedType.U4)]
275 public uint ulRawButtons;
276 [MarshalAs(UnmanagedType.U4)]
279 [MarshalAs(UnmanagedType.U4)]
282 [MarshalAs(UnmanagedType.U4)]
284 public uint ulExtraInformation;
287 [StructLayout(LayoutKind.Sequential, Pack = 1)]
288 public struct RAWKEYBOARD
290 [MarshalAs(UnmanagedType.U2)]
291 public ushort MakeCode;
292 [MarshalAs(UnmanagedType.U2)]
294 [MarshalAs(UnmanagedType.U2)]
295 public ushort Reserved;
296 [MarshalAs(UnmanagedType.U2)]
298 [MarshalAs(UnmanagedType.U4)]
300 [MarshalAs(UnmanagedType.U4)]
301 public uint ExtraInformation;
305 [StructLayout(LayoutKind.Explicit, Pack = 1)]
306 public struct RAWINPUT
309 public RAWINPUTHEADER header;
311 public RAWMOUSE mouse;
313 public RAWKEYBOARD keyboard;
319 [StructLayout(LayoutKind.Sequential, Pack = 1)]
320 public struct RID_DEVICE_INFO_MOUSE
323 public uint dwNumberOfButtons;
324 public uint dwSampleRate;
325 public bool fHasHorizontalWheel;
329 [StructLayout(LayoutKind.Sequential, Pack = 1)]
330 public struct RID_DEVICE_INFO_KEYBOARD
333 public uint dwSubType;
334 public uint dwKeyboardMode;
335 public uint dwNumberOfFunctionKeys;
336 public uint dwNumberOfIndicators;
337 public uint dwNumberOfKeysTotal;
340 [StructLayout(LayoutKind.Sequential, Pack = 1)]
341 public struct RID_DEVICE_INFO_HID
343 public uint dwVendorId;
344 public uint dwProductId;
345 public uint dwVersionNumber;
346 public ushort usUsagePage;
347 public ushort usUsage;
350 [StructLayout(LayoutKind.Explicit, Pack = 1)]
351 public struct RID_DEVICE_INFO
356 [MarshalAsAttribute(UnmanagedType.U4)]
357 public RawInputDeviceType dwType;
359 public RID_DEVICE_INFO_MOUSE mouse;
361 public RID_DEVICE_INFO_KEYBOARD keyboard;
363 public RID_DEVICE_INFO_HID hid;