-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouseHook.cs
80 lines (72 loc) · 2.98 KB
/
MouseHook.cs
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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using static Hook.WinAPI;
namespace Hook {
public delegate bool MouseEventCallback(MouseEventType type, int x, int y);
public delegate bool MouseScrollEventCallback(MouseScrollType type);
public class MouseHook {
private static IntPtr _hookID = IntPtr.Zero;
private static LowLevelProc _proc;
public static event MouseEventCallback MouseDown;
public static event MouseEventCallback MouseUp;
public static event MouseEventCallback MouseMove;
public static event MouseScrollEventCallback MouseScroll;
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
int intw = (int)wParam;
if (nCode >= 0 &&
intw == WM_LBUTTONDOWN || intw == WM_RBUTTONDOWN ||
intw == WM_LBUTTONUP || intw == WM_RBUTTONUP ||
intw == WM_MOUSEWHEEL || intw == WM_MOUSEMOVE) {
var hookStruct = Marshal.PtrToStructure<MSLLHOOKSTRUCT>(lParam);
int x = hookStruct.pt.x, y = hookStruct.pt.y;
bool res = true;
switch ((int)wParam) {
case WM_LBUTTONDOWN:
res = MouseDown?.Invoke(MouseEventType.LEFT, x, y) ?? true;
break;
case WM_RBUTTONDOWN:
res = MouseDown?.Invoke(MouseEventType.RIGHT, x, y) ?? true;
break;
case WM_LBUTTONUP:
res = MouseUp?.Invoke(MouseEventType.LEFT, x, y) ?? true;
break;
case WM_RBUTTONUP:
res = MouseUp?.Invoke(MouseEventType.RIGHT, x, y) ?? true;
break;
case WM_MOUSEMOVE:
res = MouseMove?.Invoke(MouseEventType.NONE, x, y) ?? true;
break;
case WM_MOUSEWHEEL:
res = MouseScroll?.Invoke((int)hookStruct.mouseData > 0 ? MouseScrollType.UP : MouseScrollType.DOWN) ?? true;
break;
}
if (!res)
return (IntPtr)1;
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
public static bool HookStart() {
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule) {
var handle = GetModuleHandle("user32");
_proc = HookCallback;
_hookID = SetWindowsHookEx(WH_MOUSE_LL, _proc, handle, 0);
return _hookID != IntPtr.Zero;
}
}
public static void HookEnd() {
UnhookWindowsHookEx(_hookID);
}
}
public enum MouseEventType {
NONE,
LEFT,
RIGHT,
WHEEL
}
public enum MouseScrollType {
UP = 1,
DOWN = -1
}
}