forked from coldKey1/mouse-hooks
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathglobal-mouse-events.cc
189 lines (157 loc) · 6.1 KB
/
global-mouse-events.cc
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
#define WIN32_LEAN_AND_MEAN 1
#include <napi.h>
#include <windows.h>
#include <string>
#include <iostream>
#include <atomic>
Napi::ThreadSafeFunction _tsfn;
HANDLE _hThread;
std::atomic_bool captureMouseMove = false;
// PostThreadMessage races with the actual thread; we'll get a thread ID
// and won't be able to post to it because it's "invalid" during the early
// lifecycle of the thread. To ensure that immediate pauses don't get dropped,
// we'll use this flag instead of distinct message IDs.
std::atomic_bool installEventHook = false;
DWORD dwThreadID = 0;
struct MouseEventContext {
public:
int nCode;
WPARAM wParam;
LONG ptX;
LONG ptY;
DWORD mouseData;
};
void onMainThread(Napi::Env env, Napi::Function function, MouseEventContext *pMouseEvent) {
auto nCode = pMouseEvent->nCode;
auto wParam = pMouseEvent->wParam;
auto ptX = pMouseEvent->ptX;
auto ptY = pMouseEvent->ptY;
auto nMouseData = pMouseEvent->mouseData;
delete pMouseEvent;
if (nCode >= 0) {
auto name = "";
auto button = -1;
// Isolate mouse movement, as it's more CPU intensive
if (wParam == WM_MOUSEMOVE) {
// Is mouse movement
if(captureMouseMove.load()) {
name = "mousemove";
}
} else {
// Is not mouse movement
// Determine event type
if (wParam == WM_LBUTTONUP || wParam == WM_RBUTTONUP || wParam == WM_MBUTTONUP || wParam == WM_XBUTTONUP) {
name = "mouseup";
} else if (wParam == WM_LBUTTONDOWN || wParam == WM_RBUTTONDOWN || wParam == WM_MBUTTONDOWN || wParam == WM_XBUTTONDOWN) {
name = "mousedown";
} else if (wParam == WM_MOUSEWHEEL || wParam == WM_MOUSEHWHEEL) {
name = "mousewheel";
}
// Determine button
if (wParam == WM_LBUTTONUP || wParam == WM_LBUTTONDOWN) {
button = 1;
} else if (wParam == WM_RBUTTONUP || wParam == WM_RBUTTONDOWN) {
button = 2;
} else if (wParam == WM_MBUTTONUP || wParam == WM_MBUTTONDOWN) {
button = 3;
} else if (wParam == WM_MOUSEWHEEL) {
button = 0;
} else if (wParam == WM_MOUSEHWHEEL) {
button = 1;
}
}
// Only proceed if an event was identified
if (name != "") {
Napi::HandleScope scope(env);
auto x = Napi::Number::New(env, ptX);
auto y = Napi::Number::New(env, ptY);
auto mouseData = Napi::Number::New(env, nMouseData);
// Yell back to NodeJS
function.Call(env.Global(),
{Napi::String::New(env, name), x, y,
Napi::Number::New(env, button), mouseData});
}
}
}
LRESULT CALLBACK HookCallback(int nCode, WPARAM wParam, LPARAM lParam) {
// If not WM_MOUSEMOVE or WM_MOUSEMOVE has been requested, process event
if(!(wParam == WM_MOUSEMOVE && !captureMouseMove.load())) {
// Prepare data to be processed
MSLLHOOKSTRUCT *data = (MSLLHOOKSTRUCT *)lParam;
auto pMouseEvent = new MouseEventContext();
pMouseEvent->nCode = nCode;
pMouseEvent->wParam = wParam;
pMouseEvent->ptX = data->pt.x;
pMouseEvent->ptY = data->pt.y;
pMouseEvent->mouseData = data->mouseData;
// Process event on non-blocking thread
_tsfn.NonBlockingCall(pMouseEvent, onMainThread);
}
// Let Windows continue with this event as normal
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
DWORD WINAPI MouseHookThread(LPVOID lpParam) {
MSG msg;
HHOOK hook = installEventHook.load() ? SetWindowsHookEx(WH_MOUSE_LL, HookCallback, NULL, 0) : NULL;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
if (msg.message != WM_USER) continue;
if (!installEventHook.load() && hook != NULL) {
if (!UnhookWindowsHookEx(hook)) break;
hook = NULL;
} else if (installEventHook.load() && hook == NULL) {
hook = SetWindowsHookEx(WH_MOUSE_LL, HookCallback, NULL, 0);
if (hook == NULL) break;
}
}
_tsfn.Release();
return GetLastError();
}
Napi::Boolean createMouseHook(const Napi::CallbackInfo &info) {
_hThread = CreateThread(NULL, 0, MouseHookThread, NULL, CREATE_SUSPENDED, &dwThreadID);
_tsfn = Napi::ThreadSafeFunction::New(
info.Env(),
info[0].As<Napi::Function>(),
"WH_MOUSE_LL Hook Thread",
512,
1,
[] ( Napi::Env ) { CloseHandle(_hThread); }
);
ResumeThread(_hThread);
return Napi::Boolean::New(info.Env(), true);
}
void enableMouseMove(const Napi::CallbackInfo &info) {
captureMouseMove = true;
}
void disableMouseMove(const Napi::CallbackInfo &info) {
captureMouseMove = false;
}
Napi::Boolean pauseMouseEvents(const Napi::CallbackInfo &info) {
BOOL bDidPost = FALSE;
if (dwThreadID != 0) {
installEventHook = false;
bDidPost = PostThreadMessageW(dwThreadID, WM_USER, NULL, NULL);
}
return Napi::Boolean::New(info.Env(), bDidPost);
}
Napi::Boolean resumeMouseEvents(const Napi::CallbackInfo &info) {
BOOL bDidPost = FALSE;
if (dwThreadID != 0) {
installEventHook = true;
bDidPost = PostThreadMessageW(dwThreadID, WM_USER, NULL, NULL);
}
return Napi::Boolean::New(info.Env(), bDidPost);
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "createMouseHook"),
Napi::Function::New(env, createMouseHook));
exports.Set(Napi::String::New(env, "enableMouseMove"),
Napi::Function::New(env, enableMouseMove));
exports.Set(Napi::String::New(env, "disableMouseMove"),
Napi::Function::New(env, disableMouseMove));
exports.Set(Napi::String::New(env, "pauseMouseEvents"),
Napi::Function::New(env, pauseMouseEvents));
exports.Set(Napi::String::New(env, "resumeMouseEvents"),
Napi::Function::New(env, resumeMouseEvents));
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)