-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWin32.cs
More file actions
387 lines (335 loc) · 10.1 KB
/
Win32.cs
File metadata and controls
387 lines (335 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Mike ( lutinore@hotmail.fr ) - May 2007 - RawInput.NET.dll - Win32.cs
using System;
using System.Security;
using System.Runtime.InteropServices;
namespace MultipleMice.Native
{
[Flags]
internal enum PenStatus : byte
{
PenTipDown = 0x01,
SideSwitchDown =0x02,
Inverted = 0x04,
EraserDown = 0x08,
Reserved = 0x18,
InRange = 0x20,
ControlData = 0x40,
Sync = 0x80
}
[StructLayout(LayoutKind.Sequential)]
internal struct PEN_DATA
{
public byte ReportID;
public PenStatus Status;
public ushort X;
public ushort Y;
public ushort Pressure;
public byte XTilt;
public byte YTilt;
public ushort Firmware;
public int Data;
public override string ToString()
{
return string.Format("Report ID: {0}, Status: {1}, X-Y: {2}x{3}, Pressure: {4}, Tilt X-Y: {5}x{6}, Firmware: {7}, Data: {8}",
ReportID, Status, X, Y, Pressure, XTilt, YTilt, Firmware, Data);
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct RAWINPUTDEVICELIST
{
public IntPtr hDevice;
public uint dwType;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RID_DEVICE_INFO_MOUSE
{
public uint dwId;
public uint dwNumberOfButtons;
public uint dwSampleRate;
public int fHasHorizontalWheel; // Vista.
}
[StructLayout(LayoutKind.Sequential)]
internal struct RID_DEVICE_INFO_KEYBOARD
{
public uint dwType;
public uint dwSubType;
public uint dwKeyboardMode;
public uint dwNumberOfFunctionKeys;
public uint dwNumberOfIndicators;
public uint dwNumberOfKeysTotal;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RID_DEVICE_INFO_HID
{
public uint dwVendorId;
public uint dwProductId;
public uint dwVersionNumber;
public ushort usUsagePage;
public ushort usUsage;
}
[StructLayout(LayoutKind.Explicit)]
internal struct RID_DEVICE_INFO
{
// Fields
[FieldOffset(0)]
public uint cbSize;
[FieldOffset(4)]
public uint dwType;
[FieldOffset(8)]
public RID_DEVICE_INFO_HID hid;
[FieldOffset(8)]
public RID_DEVICE_INFO_KEYBOARD keyboard;
[FieldOffset(8)]
public RID_DEVICE_INFO_MOUSE mouse;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RAWINPUTDEVICE
{
public ushort usUsagePage;
public ushort usUsage;
public uint dwFlags;
public IntPtr hwndTarget;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RAWINPUTHEADER
{
public uint dwType;
public uint dwSize;
public IntPtr hDevice;
public IntPtr wParam;
}
[StructLayout(LayoutKind.Explicit)]
internal struct RAWMOUSE
{
[FieldOffset(0)]
public ushort usFlags;
[FieldOffset(4)] // Alignment.
public uint ulButtons;
[FieldOffset(4)] // Union.
public ushort usButtonFlags;
[FieldOffset(6)]
public ushort usButtonData;
[FieldOffset(8)]
public uint ulRawButtons;
[FieldOffset(12)]
public int lLastX;
[FieldOffset(16)]
public int lLastY;
[FieldOffset(20)]
public uint ulExtraInformation;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RAWKEYBOARD
{
public ushort MakeCode;
public ushort Flags;
public ushort Reserved;
public ushort VKey;
public uint Message;
public uint ExtraInformation;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RAWHID
{
public uint dwSizeHid;
public uint dwCount;
public byte bRawData; // fixed byte bRawData[ 1 ];
}
internal interface IRAWINPUT
{
RAWINPUTHEADER Header { get; set; }
RAWMOUSE Mouse { get; set; }
RAWKEYBOARD Keyboard { get; set; }
RAWHID Hid { get; set; }
}
[StructLayout(LayoutKind.Explicit)]
internal struct RAWINPUT32 : IRAWINPUT
{
[FieldOffset(0)] public RAWINPUTHEADER header;
[FieldOffset(16)] public RAWMOUSE mouse;
[FieldOffset(16)] // Union.
public RAWKEYBOARD keyboard;
[FieldOffset(16)] // Union.
public RAWHID hid;
public RAWINPUTHEADER Header
{
get { return header; }
set { header = value; }
}
public RAWMOUSE Mouse
{
get { return mouse; }
set { mouse = value; }
}
public RAWKEYBOARD Keyboard
{
get { return keyboard; }
set { keyboard = value; }
}
public RAWHID Hid
{
get { return hid; }
set { hid = value; }
}
}
[StructLayout(LayoutKind.Explicit)]
internal struct RAWINPUT64 : IRAWINPUT
{
[FieldOffset(0)] public RAWINPUTHEADER header;
[FieldOffset(24)] public RAWMOUSE mouse;
[FieldOffset(24)] // Union.
public RAWKEYBOARD keyboard;
[FieldOffset(24)] // Union.
public RAWHID hid;
public RAWINPUTHEADER Header
{
get { return header; }
set { header = value; }
}
public RAWMOUSE Mouse
{
get { return mouse; }
set { mouse = value; }
}
public RAWKEYBOARD Keyboard
{
get { return keyboard; }
set { keyboard = value; }
}
public RAWHID Hid
{
get { return hid; }
set { hid = value; }
}
}
[SuppressUnmanagedCodeSecurity]
internal static class Win32
{
public const int HWND_MESSAGE = -3;
public const int WM_INPUT = 0x00FF;
public const int RIM_TYPEMOUSE = 0;
public const int RIM_TYPEKEYBOARD = 1;
public const int RIM_TYPEHID = 2;
//public const int RIDI_PREPARSEDDATA = 0x20000005;
public const int RIDI_DEVICENAME = 0x20000007;
public const int RIDI_DEVICEINFO = 0x2000000b;
public const int RIDEV_REMOVE = 0x00000001;
//public const int RIDEV_EXCLUDE = 0x00000010;
//public const int RIDEV_PAGEONLY = 0x00000020;
//public const int RIDEV_NOLEGACY = 0x00000030;
//public const int RIDEV_INPUTSINK = 0x00000100;
//public const int RIDEV_CAPTUREMOUSE = 0x00000200;
//public const int RIDEV_NOHOTKEYS = 0x00000200;
//public const int RIDEV_APPKEYS = 0x00000400;
//public const int RIDEV_EXINPUTSINK = 0x00001000;
//public const int RIDEV_DEVNOTIFY = 0x00002000;
//public const int RIM_INPUT = 0;
public const int RIM_INPUTSINK = 1;
public const int RID_INPUT = 0x10000003;
public const int RID_HEADER = 0x10000005;
//public const ushort GENERIC_DESKTOP_PAGE = 0x01;
//public const ushort MOUSE_USAGE = 0x02;
//public const ushort KEYBOARD_USAGE = 0x06;
public const int RI_MOUSE_WHEEL = 0x0400;
public static readonly uint SIZEOF_RAWINPUTDEVICELIST =
(uint)Marshal.SizeOf(typeof(RAWINPUTDEVICELIST));
public static readonly uint SIZEOF_RID_DEVICE_INFO =
(uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
public static readonly uint SIZEOF_RAWINPUTDEVICE =
(uint)Marshal.SizeOf(typeof(RAWINPUTDEVICE));
public static readonly uint SIZEOF_RAWINPUTHEADER =
(uint)Marshal.SizeOf(typeof(RAWINPUTHEADER));
public static readonly uint SIZEOF_RAWINPUT64 =
(uint)Marshal.SizeOf(typeof(RAWINPUT64));
public static readonly uint SIZEOF_RAWINPUT32 =
(uint)Marshal.SizeOf(typeof(RAWINPUT32));
[SuppressUnmanagedCodeSecurity, SecurityCritical, DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint GetRawInputDeviceList([In, Out] RAWINPUTDEVICELIST[] ridl, [In, Out] ref uint numDevices, uint sizeInBytes);
[SecurityCritical, SuppressUnmanagedCodeSecurity, DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint GetRawInputDeviceInfo(IntPtr hDevice, uint command, [In] ref RID_DEVICE_INFO ridInfo, ref uint sizeInBytes);
[SecurityCritical, SuppressUnmanagedCodeSecurity, DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint GetRawInputDeviceInfo(IntPtr hDevice, uint command, [In] IntPtr ridInfo, ref uint sizeInBytes);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RegisterRawInputDevices
(
IntPtr pRawInputDevices, // [ In ] RAWINPUTDEVICE[ ]
uint uiNumDevices,
uint cbSize
);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetRegisteredRawInputDevices
(
IntPtr pRawInputDevices, // [ In, Out ] RAWINPUTDEVICE[ ]
ref uint puiNumDevices,
uint cbSize
);
[DllImport("user32.dll")]
public static extern uint GetRawInputData
(
IntPtr hRawInput,
uint uiCommand,
IntPtr pData, // [ In, Out ] byte[ ] // ref RAWINPUTHEADER
ref uint pcbSize,
uint cbSizeHeader
);
[DllImport("user32.dll")]
public static extern uint GetRawInputData
(
IntPtr hRawInput,
uint uiCommand,
[Out] out RAWINPUT32 input,
ref uint pcbSize,
uint cbSizeHeader
);
[DllImport("user32.dll")]
public static extern uint GetRawInputData
(
IntPtr hRawInput,
uint uiCommand,
[Out] out RAWINPUT64 input,
ref uint pcbSize,
uint cbSizeHeader
);
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
public POINT()
{
}
public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
}
[DllImport("user32.dll")]
public static extern uint SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int nWidth, int nHeight, uint uFlags);
public const UInt32 SWP_NOSIZE = 0x0001;
public const UInt32 SWP_NOMOVE = 0x0002;
public const UInt32 SWP_NOZORDER = 0x0004;
public const UInt32 SWP_NOREDRAW = 0x0008;
public const UInt32 SWP_NOACTIVATE = 0x0010;
public const UInt32 SWP_FRAMECHANGED = 0x0020; /* The frame changed: send WM_NCCALCSIZE */
public const UInt32 SWP_SHOWWINDOW = 0x0040;
public const UInt32 SWP_HIDEWINDOW = 0x0080;
public const UInt32 SWP_NOCOPYBITS = 0x0100;
public const UInt32 SWP_NOOWNERZORDER = 0x0200; /* Don't do owner Z ordering */
public const UInt32 SWP_NOSENDCHANGING = 0x0400; /* Don't send WM_WINDOWPOSCHANGING */
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("User32.dll")]
internal static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
static extern bool GetCursorPos([In, Out] POINT pt);
public static POINT GetCursorPosition()
{
POINT result = new POINT();
GetCursorPos(result);
return result;
}
}
}
// Wrapper.Windows