forked from Phaiax/Key-n-Stroke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMouseHook.cs
158 lines (127 loc) · 4.43 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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KeyNStroke;
namespace KeyNStroke
{
/// <summary>
/// Low level mouse event interception class
/// </summary>
public class MouseHook : IDisposable, IMouseRawEventProvider
{
#region Initializion
int doubleClickTime;
/// <summary>
/// Set up the mouse hook
/// </summary>
public MouseHook()
{
doubleClickTime = NativeMethodsMouse.GetDoubleClickTime();
Log.e("ME", "doubleClickTime " + doubleClickTime.ToString());
RegisterMouseHook();
}
#endregion
#region Hook Add/Remove
//Variables used in the call to SetWindowsHookEx
private NativeMethodsMouse.HookHandlerDelegate proc;
private IntPtr hookID = IntPtr.Zero;
/// <summary>
/// Registers the function HookCallback for the global mouse events winapi
/// </summary>
private void RegisterMouseHook()
{
if (hookID != IntPtr.Zero)
return;
proc = new NativeMethodsMouse.HookHandlerDelegate(HookCallback);
using (Process curProcess = Process.GetCurrentProcess())
{
using (ProcessModule curModule = curProcess.MainModule)
{
hookID = NativeMethodsMouse.SetWindowsHookEx(NativeMethodsMouse.WH_MOUSE_LL, proc,
NativeMethodsKeyboard.GetModuleHandle(curModule.ModuleName), 0);
}
}
}
/// <summary>
/// Unregisters the winapi hook for global mouse events
/// </summary>
private void UnregisterMouseHook()
{
if (hookID == IntPtr.Zero)
return;
NativeMethodsMouse.UnhookWindowsHookEx(hookID);
hookID = IntPtr.Zero;
}
#endregion
#region Event Handling
MouseRawEventArgs lastDownEvent;
/// <summary>
/// Processes the key event captured by the hook.
/// </summary>
private IntPtr HookCallback(int nCode,
UIntPtr wParam,
ref NativeMethodsMouse.MSLLHOOKSTRUCT lParam)
{
if (nCode == NativeMethodsMouse.HC_ACTION)
{
MouseRawEventArgs args = new MouseRawEventArgs(lParam);
args.ParseWparam(wParam);
CheckDoubleClick(args);
Log.e("ME", String.Format("MOUSE: Button:{0} Action:{1} Orig:{2}",
args.Button.ToString(), args.Action.ToString(),
args.Event.ToString()));
OnMouseEvent(args);
}
return NativeMethodsMouse.CallNextHookEx(hookID, nCode, wParam, ref lParam);
}
private void CheckDoubleClick(MouseRawEventArgs args)
{
if (lastDownEvent != null && args.Action == MouseAction.Down)
{
if (args.Button == lastDownEvent.Button
&& args.Msllhookstruct.time <= lastDownEvent.Msllhookstruct.time + doubleClickTime
&& args.Msllhookstruct.pt.Equals(lastDownEvent.Msllhookstruct.pt))
{
args.Action = MouseAction.DblClk;
Log.e("ME", "DBLCLK");
}
}
if (args.Action == MouseAction.Down)
lastDownEvent = args;
}
#endregion
#region Event Forwarding
/// <summary>
/// Fires if mouse is moved or clicked.
/// </summary>
public event MouseRawEventHandler MouseEvent;
/// <summary>
/// Raises the MouseEvent event.
/// </summary>
/// <param name="e">An instance of MouseRawEventArgs</param>
public void OnMouseEvent(MouseRawEventArgs e)
{
if (MouseEvent != null)
MouseEvent(e);
}
#endregion
#region Finalizing and Disposing
~MouseHook()
{
Log.e("HOOK", "~MouseHook");
UnregisterMouseHook();
}
/// <summary>
/// Releases the mouse hook.
/// </summary>
public void Dispose()
{
Log.e("HOOK", "Dispose MouseHook");
UnregisterMouseHook();
}
#endregion
}
}