forked from a1928370421/Bongobs-Cat-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hook.cpp
421 lines (387 loc) · 7.24 KB
/
Hook.cpp
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "Hook.hpp"
#include <Windows.h>
#include "time.h"
#include "string"
#include <sstream>
#include <fstream>
#include <iostream>
#include "VtuberDelegate.hpp"
#include "View.hpp"
#include<thread>
#include"EventManager.hpp"
#include <tchar.h>
#ifndef HID_USAGE_PAGE_GENERIC
#define HID_USAGE_PAGE_GENERIC ((USHORT)0x01)
#endif
#ifndef HID_USAGE_GENERIC_MOUSE
#define HID_USAGE_GENERIC_MOUSE ((USHORT)0x02)
#endif
static HHOOK hhkLowLevelKybd;
static HHOOK hhkLowLevelMs;
static int HookCode(DWORD code)
{
/*
Translate the return code from hook and
return the std::string rep of the the code.
ex. 0x88 -> "[SHIFT]"
caps = Caps lock on
shift = Shift key pressed
WinUser.h = define statments
LINK = https://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx
*/
int key=-1;
switch (code) // SWITCH ON INT
{
// Char keys for ASCI
// No VM Def in header
case 0x41:
key = 0;//a
break;
case 0x42:
key = 1;
break;
case 0x43:
key = 2;
break;
case 0x44:
key = 3;
break;
case 0x45:
key = 4;
break;
case 0x46:
key = 5;
break;
case 0x47:
key = 6;
break;
case 0x48:
key = 7;
break;
case 0x49:
key =8;
break;
case 0x4A:
key = 9;
break;
case 0x4B:
key = 10;
break;
case 0x4C:
key = 11;
break;
case 0x4D:
key = 12;
break;
case 0x4E:
key = 13;
break;
case 0x4F:
key = 14;
break;
case 0x50:
key = 15;
break;
case 0x51:
key = 16;
break;
case 0x52:
key = 17;
break;
case 0x53:
key = 18;
break;
case 0x54:
key = 19;
break;
case 0x55:
key = 20;
break;
case 0x56:
key = 21;
break;
case 0x57:
key = 22;
break;
case 0x58:
key = 23;
break;
case 0x59:
key = 24;
break;
case 0x5A:
key = 25; //z
break;
// Num Keyboard
case 0x30:
key = 26; //0 key
break;
case 0x31:
key = 27;// 1 key
break;
case 0x32:
key = 28;// 2 key
break;
case 0x33:
key = 29;// 3 key
break;
case 0x34:
key = 30;
break;
case 0x35:
key =31;
break;
case 0x36:
key = 32;
break;
case 0x37:
key = 33;
break;
case 0x38:
key = 34;
break;
case 0x39:
key = 35;// 9 key
break;
case VK_SPACE:
key = 36;
break;
case VK_LSHIFT:
key = 37;
break;
case VK_LCONTROL:
key = 38;
break;
case VK_F1:
key = 39;
break;
case VK_F2:
key = 40;
break;
case VK_F3:
key = 41;
break;
case VK_F4:
key = 42;
break;
case VK_F5:
key = 43;
break;
case VK_F6:
key = 44;
break;
case VK_F7:
key = 45;
break;
case VK_F8:
key = 46;
break;
case VK_F9:
key = 47;
break;
case VK_F10:
key = 48;
break;
case VK_F11:
key = 49;
break;
case VK_F12:
key = 50;
break;
case VK_UP:
key = 51;
break;
case VK_DOWN:
key = 52;
break;
case VK_RIGHT:
key = 53;
break;
case VK_LEFT:
key = 54;
break;
case VK_OEM_COMMA:
key = 55;
break;
case VK_OEM_PERIOD:
key = 56;
break;
case VK_OEM_4:
key = 57;
break;
case VK_OEM_6:
key = 58;
break;
case VK_RSHIFT:
key = 59;
break;
case VK_RCONTROL:
key = 60;
break;
default:
key = -1;
break;
}
return key;
}
LRESULT CALLBACK Hook::KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
EventManager *eventManager =VtuberDelegate::GetInstance()->GetView()->GetEventManager();
KBDLLHOOKSTRUCT *p = (KBDLLHOOKSTRUCT *)lParam;
if (nCode == HC_ACTION) {
if (wParam == WM_KEYUP) {
int re =HookCode(p->vkCode);
if (re>=0) {
eventManager->KeyEventUp(re);
}
}
if (wParam == WM_KEYDOWN) {
int re = HookCode(p->vkCode);
if (re >= 0) {
eventManager->KeyEventDown(re);
}
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
LRESULT CALLBACK Hook::MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
EventManager *eventManager =VtuberDelegate::GetInstance()->GetView()->GetEventManager();
PMSLLHOOKSTRUCT p = (PMSLLHOOKSTRUCT)lParam;
if (nCode == HC_ACTION) {
switch (wParam) {
case WM_LBUTTONUP: {
eventManager->LeftButtonUp();
break;
}
case WM_LBUTTONDOWN: {
eventManager->LeftButtonDown();
break;
}
case WM_RBUTTONDOWN: {
eventManager->RightButtonDown();
break;
}
case WM_RBUTTONUP: {
eventManager->RightButtonUp();
break;
}
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
LRESULT Hook::WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_COMMAND:
break;
case WM_PAINT:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_INPUT: {
UINT dwSize = 0;
GetRawInputData((HRAWINPUT)lParam, (UINT)RID_INPUT, NULL,
&dwSize,
sizeof(RAWINPUTHEADER));
LPBYTE lpbBuffer = new BYTE[dwSize];
GetRawInputData(
(HRAWINPUT)lParam, (UINT)RID_INPUT, (LPVOID)lpbBuffer,
(PUINT)&dwSize,
(UINT)sizeof(RAWINPUTHEADER));
RAWINPUT *raw = (RAWINPUT *)lpbBuffer;
if (raw->header.dwType == RIM_TYPEMOUSE)
{
EventManager *eventManager =VtuberDelegate::GetInstance()
->GetView()
->GetEventManager();
int xPosRelative = raw->data.mouse.lLastX;
int yPosRelative = raw->data.mouse.lLastY;
eventManager->SetRelativeMouse(xPosRelative,yPosRelative);
}
delete[] lpbBuffer;
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Hook::Hook() {}
Hook::~Hook() {}
void Hook::Strat() {
isExist = false;
th = new std::thread(&Hook::Run, this);
th->detach();
}
void Hook::Stop() {
isExist = true;
UnhookWindowsHookEx(hhkLowLevelKybd);
UnhookWindowsHookEx(hhkLowLevelMs);
}
void Hook::Run() {
hhkLowLevelKybd =SetWindowsHookEx(WH_KEYBOARD_LL, Hook::KeyboardHookProc, 0, 0);
hhkLowLevelMs =SetWindowsHookEx(WH_MOUSE_LL, Hook::MouseHookProc, 0, 0);
HINSTANCE hInst;
hInst = GetModuleHandle(NULL);
WNDCLASSEX wcx;
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC |
CS_NOCLOSE;
wcx.lpfnWndProc = WindowProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInst;
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)WHITE_BRUSH;
wcx.lpszMenuName = NULL;
wcx.lpszClassName = _T("BONGOHOOK");
wcx.hIconSm = NULL;
if (!RegisterClassEx(&wcx)) {
printf("RegisterClassEx failed");
return;
}
//set windows position
int OSDleft = GetSystemMetrics(SM_CXSCREEN) / 2 - 300;
int OSDTop = GetSystemMetrics(SM_CYSCREEN) / 2;
m_hWnd = CreateWindowEx(
WS_EX_LAYERED | WS_EX_TOOLWINDOW | WS_EX_TOPMOST |
WS_EX_TRANSPARENT | WS_EX_NOACTIVATE,
wcx.lpszClassName,
NULL,
WS_VISIBLE | WS_POPUP,
OSDleft,
OSDTop,
300,
300,
(HWND)NULL,
(HMENU)NULL,
hInst,
(LPVOID)NULL);
if (!m_hWnd) {
//fail to creat window;
return;
}
//regist raw input device
RAWINPUTDEVICE Rid[1];
Rid[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
Rid[0].usUsage = HID_USAGE_GENERIC_MOUSE;
Rid[0].dwFlags = RIDEV_INPUTSINK;
Rid[0].hwndTarget = m_hWnd;
if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE) {
return;
};
//message loop
BOOL bRet;
MSG msg;
while ((bRet = GetMessage(&msg, 0, 0, 0)) != 0) {
if (bRet == -1||isExist) {
break;
} else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}