-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseHelper.cs
More file actions
348 lines (286 loc) · 10.4 KB
/
Copy pathMouseHelper.cs
File metadata and controls
348 lines (286 loc) · 10.4 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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MouseClicker
{
/// <summary>
/// 鼠标按键枚举
/// </summary>
public enum MouseButton
{
Left = 0,
Right = 1,
Middle = 2,
X1 = 3, // 侧键1
X2 = 4 // 侧键2
}
/// <summary>
/// 鼠标操作辅助类
/// </summary>
public static class MouseHelper
{
#region Windows API 导入
[DllImport("user32.dll")]
private static extern void SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
[DllImport("user32.dll")]
private static extern bool GetCursorPos(out POINT lpPoint);
[DllImport("user32.dll")]
private static extern bool SetCursorPos(int X, int Y);
[DllImport("winmm.dll")]
private static extern uint timeBeginPeriod(uint uPeriod);
[DllImport("winmm.dll")]
private static extern uint timeEndPeriod(uint uPeriod);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, IntPtr lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string lpModuleName);
#endregion
#region 结构体定义
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
private struct INPUT
{
public uint type;
public MOUSEINPUT mi;
}
[StructLayout(LayoutKind.Sequential)]
private struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
#endregion
#region 常量定义
private const int INPUT_MOUSE = 0;
private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
private const uint MOUSEEVENTF_LEFTUP = 0x0004;
private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
private const uint MOUSEEVENTF_RIGHTUP = 0x0010;
private const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
private const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
private const uint MOUSEEVENTF_XDOWN = 0x0080;
private const uint MOUSEEVENTF_XUP = 0x0100;
private const uint XBUTTON1 = 0x0001;
private const uint XBUTTON2 = 0x0002;
private const int WH_MOUSE_LL = 14;
private const int WH_KEYBOARD_LL = 13;
private const int WM_LBUTTONDOWN = 0x0201;
private const int WM_LBUTTONUP = 0x0202;
private const int WM_RBUTTONDOWN = 0x0204;
private const int WM_RBUTTONUP = 0x0205;
private const int WM_MBUTTONDOWN = 0x0207;
private const int WM_MBUTTONUP = 0x0208;
private const int WM_XBUTTONDOWN = 0x020B;
private const int WM_XBUTTONUP = 0x020C;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private const int WM_SYSKEYDOWN = 0x0104;
private const int WM_SYSKEYUP = 0x0105;
#endregion
#region 委托定义
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
#endregion
#region 私有字段
private static IntPtr _mouseHook = IntPtr.Zero;
private static IntPtr _keyboardHook = IntPtr.Zero;
private static LowLevelMouseProc _mouseProc;
private static LowLevelKeyboardProc _keyboardProc;
#endregion
#region 公共事件
public static event EventHandler<MouseButtonEventArgs> GlobalMouseDown;
public static event EventHandler<KeyEventArgs> GlobalKeyDown;
#endregion
#region 计时器精度控制
/// <summary>
/// 提高系统计时器精度到1ms
/// </summary>
public static void BeginHighResolution()
{
timeBeginPeriod(1);
}
/// <summary>
/// 恢复系统计时器精度
/// </summary>
public static void EndHighResolution()
{
timeEndPeriod(1);
}
#endregion
#region 鼠标位置操作
/// <summary>
/// 获取当前鼠标位置
/// </summary>
public static (int X, int Y) GetPosition()
{
GetCursorPos(out POINT point);
return (point.X, point.Y);
}
/// <summary>
/// 设置鼠标位置
/// </summary>
public static void SetPosition(int x, int y)
{
SetCursorPos(x, y);
}
#endregion
#region 鼠标点击操作
/// <summary>
/// 执行鼠标点击
/// </summary>
public static void Click(MouseButton button, int x = -1, int y = -1)
{
if (x >= 0 && y >= 0)
{
SetPosition(x, y);
}
INPUT[] inputs = new INPUT[2];
uint downFlag = 0, upFlag = 0;
uint data = 0;
switch (button)
{
case MouseButton.Left:
downFlag = MOUSEEVENTF_LEFTDOWN;
upFlag = MOUSEEVENTF_LEFTUP;
break;
case MouseButton.Right:
downFlag = MOUSEEVENTF_RIGHTDOWN;
upFlag = MOUSEEVENTF_RIGHTUP;
break;
case MouseButton.Middle:
downFlag = MOUSEEVENTF_MIDDLEDOWN;
upFlag = MOUSEEVENTF_MIDDLEUP;
break;
case MouseButton.X1:
downFlag = MOUSEEVENTF_XDOWN;
upFlag = MOUSEEVENTF_XUP;
data = XBUTTON1;
break;
case MouseButton.X2:
downFlag = MOUSEEVENTF_XDOWN;
upFlag = MOUSEEVENTF_XUP;
data = XBUTTON2;
break;
}
inputs[0].type = INPUT_MOUSE;
inputs[0].mi.dwFlags = downFlag;
inputs[0].mi.mouseData = data;
inputs[1].type = INPUT_MOUSE;
inputs[1].mi.dwFlags = upFlag;
inputs[1].mi.mouseData = data;
SendInput(2, inputs, Marshal.SizeOf(typeof(INPUT)));
}
/// <summary>
/// 执行鼠标双击
/// </summary>
public static void DoubleClick(MouseButton button, int x = -1, int y = -1)
{
Click(button, x, y);
Click(button, x, y);
}
#endregion
#region 全局钩子
/// <summary>
/// 安装全局鼠标和键盘钩子
/// </summary>
public static void InstallHooks()
{
_mouseProc = MouseHookProc;
_keyboardProc = KeyboardHookProc;
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
IntPtr hMod = GetModuleHandle(curModule.ModuleName);
IntPtr mouseProcPtr = Marshal.GetFunctionPointerForDelegate(_mouseProc);
IntPtr keyboardProcPtr = Marshal.GetFunctionPointerForDelegate(_keyboardProc);
_mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProcPtr, hMod, 0);
_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardProcPtr, hMod, 0);
}
}
/// <summary>
/// 卸载全局钩子
/// </summary>
public static void UninstallHooks()
{
if (_mouseHook != IntPtr.Zero)
{
UnhookWindowsHookEx(_mouseHook);
_mouseHook = IntPtr.Zero;
}
if (_keyboardHook != IntPtr.Zero)
{
UnhookWindowsHookEx(_keyboardHook);
_keyboardHook = IntPtr.Zero;
}
}
private static IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
MSLLHOOKSTRUCT hookStruct = Marshal.PtrToStructure<MSLLHOOKSTRUCT>(lParam);
MouseButton? button = null;
if (wParam == (IntPtr)WM_LBUTTONDOWN)
button = MouseButton.Left;
else if (wParam == (IntPtr)WM_RBUTTONDOWN)
button = MouseButton.Right;
else if (wParam == (IntPtr)WM_MBUTTONDOWN)
button = MouseButton.Middle;
else if (wParam == (IntPtr)WM_XBUTTONDOWN)
{
uint xButton = hookStruct.mouseData >> 16;
button = xButton == XBUTTON1 ? MouseButton.X1 : MouseButton.X2;
}
if (button.HasValue)
{
GlobalMouseDown?.Invoke(null, new MouseButtonEventArgs(button.Value));
}
}
return CallNextHookEx(_mouseHook, nCode, wParam, lParam);
}
private static IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN))
{
int vkCode = Marshal.ReadInt32(lParam);
Keys key = (Keys)vkCode;
GlobalKeyDown?.Invoke(null, new KeyEventArgs(key));
}
return CallNextHookEx(_keyboardHook, nCode, wParam, lParam);
}
#endregion
}
/// <summary>
/// 鼠标按键事件参数
/// </summary>
public class MouseButtonEventArgs : EventArgs
{
public MouseButton Button { get; }
public MouseButtonEventArgs(MouseButton button)
{
Button = button;
}
}
}