-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook.h
88 lines (62 loc) · 2.44 KB
/
hook.h
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
#ifndef HOOK_H
#define HOOK_H
#include <future>
#include <iostream>
#include <queue>
#include <vector>
#include <windows.h>
#include "input.h"
struct Event {
// if key is 0, then direction won't be populated
// otherwise, point won't be populated
int key; // virtual key code, `WM_MOUSEMOVE` for mouse movement, `WM_MOUSEWHEEL` for mouse wheel scroll
bool direction; // false for key release / scroll up, true for key press / scroll down
bool injected; // false for physical input, true for injected input
POINT point; // if key is 0, gives the position of cursor
};
// every `Function` must have an `action` function that executes the actual macro, combo etc.
// `action` returns a bool that indicated whether or not the `Event` should continue through message queue
// a `trigger` function should be defined if the action function takes a relatively long time to complete,
// so that `action` function can be run in a separate thread
// if `trigger` is defined, the return value of `action` is ignored
// instead, `trigger`'s return value will indicate if the event should be blocked
// 0 indicates the action function should not be run
// 1 indicates the action function should be run, but the event will be passed on
// 2 indicates the action function should be run, and the event will be blocked
/* class Function {
private:
bool (*action)(Event&) { nullptr };
int (*trigger)(Event&) { nullptr };
std::future<bool> thread;
Function(bool (*action)(Event&), int (*trigger)(Event&) = nullptr) :
action(action), trigger(trigger) {}
bool run(Event&);
friend class Hook;
};
*/
class Hook {
HHOOK hKeyboardHook{ nullptr };
HHOOK hMouseHook{ nullptr };
PKBDLLHOOKSTRUCT keyboard{ nullptr };
PMSLLHOOKSTRUCT mouse{ nullptr };
/* std::vector<Function> functions;
bool (*interrupt)(Event&) { nullptr }; */
Input input;
Event event;
bool states[256] = { 0 };
MSG msg{ nullptr };
bool (*function)(const Event&){ nullptr };
void hook();
void unhook();
public:
Hook();
~Hook();
void run(bool (*function)(const Event&));
bool hookProc(bool type, WPARAM wParam, LPARAM lParam);
/* void add(bool (*action)(Event&), int (*trigger)(Event&) = nullptr);
void set_interrupt(bool (*function)(Event&));
void remove(bool (*action)(Event&), int (*trigger)(Event&) = nullptr); */
bool state(int key);
bool state(const std::vector<int>& keys);
};
#endif