-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.h
68 lines (58 loc) · 1.66 KB
/
driver.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
#include <cstdint>
#include <Windows.h>
#include <iostream>
#include <cstdint>
#include <Windows.h>
#include <iostream>
class MouseController {
public:
enum class Button {
None = 0,
LeftButtonDown = 1,
LeftButtonUp = 2,
RightButtonDown = 4,
RightButtonUp = 8,
MiddleButtonDown = 16,
MiddleButtonUp = 32,
XButton1Down = 64,
XButton1Up = 128,
XButton2Down = 256,
XButton2Up = 512,
MouseWheel = 1024,
MouseHorizontalWheel = 2048
};
MouseController() {
hDriver = CreateFileA("\\\\.\\Oykyo", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, 0, nullptr);
if (hDriver == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to open driver.\n";
}
}
~MouseController() {
if (hDriver != INVALID_HANDLE_VALUE) {
CloseHandle(hDriver);
}
}
bool MoveMouse(int x, int y) {
return SendMouseEvent(x, y, Button::None);
}
bool Click(Button button) {
return SendMouseEvent(0, 0, button);
}
private:
HANDLE hDriver;
struct MouseRequest {
int x;
int y;
USHORT buttonFlags;
};
bool SendMouseEvent(int x, int y, Button button) {
if (hDriver == INVALID_HANDLE_VALUE) {
return false;
}
MouseRequest request{ x, y, static_cast<USHORT>(button) };
return DeviceIoControl(hDriver, CTL_CODE(34U, 73142U, 0U, 0U),
&request, sizeof(request), nullptr, 0, nullptr, nullptr);
}
};