-
Notifications
You must be signed in to change notification settings - Fork 7
/
virtual.cpp
92 lines (73 loc) · 3.25 KB
/
virtual.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
#include "virtual.hpp"
#include "helper.hpp"
const std::map<std::string, UINT> Virtual::wParams = {{"left", MK_LBUTTON},
{"right", MK_RBUTTON},
{"middle", MK_MBUTTON},
{"x1", MK_XBUTTON1},
{"x2", MK_XBUTTON2}};
void Virtual::mousePosGetter(POINT *coords) {
GetCursorPos(coords);
ScreenToClient(hWnd, coords);
}
Napi::Value Virtual::getLastCoords(const Napi::CallbackInfo &info) {
Napi::Object coords = Napi::Object::New(info.Env());
coords["x"] = lastCoords.x;
coords["y"] = lastCoords.y;
return coords;
};
void Virtual::mbToggler(std::string button, bool isButtonDown) {
PostMessageA(hWnd, Helper::mouseEvents.at(button)[(int)isButtonDown], isButtonDown ? wParams.at(button) : 0, MAKELPARAM(lastCoords.x, lastCoords.y));
}
void Virtual::mover(POINT coords, bool isAbsolute) {
PostMessageA(hWnd, WM_MOUSEMOVE, 1, isAbsolute ? MAKELPARAM(coords.x, coords.y) : MAKELPARAM(lastCoords.x + coords.x, lastCoords.y + coords.y));
if (isAbsolute) {
lastCoords.x = coords.x;
lastCoords.y = coords.y;
} else {
lastCoords.x += coords.x;
lastCoords.y += coords.y;
}
}
void Virtual::wheelScroller(int x) {
SendMessageA(hWnd, WM_MOUSEWHEEL, MAKEWPARAM(x, MK_MBUTTON), MAKELPARAM(lastCoords.x, lastCoords.y));
}
void Virtual::keyToggler(UINT key, bool isKeyDown) {
PostMessageA(hWnd, isKeyDown ? WM_KEYDOWN : WM_KEYUP, key, 0 | (key << 16) | (0 << 24) | (0 << 29) | ((UINT)!isKeyDown << 30) | ((UINT)!isKeyDown << 31));
}
void Virtual::charPrinter(std::wstring str) {
for (size_t i = 0; i < str.size(); i++) {
SendMessageW(hWnd, WM_CHAR, str.at(i), 0);
}
}
Napi::FunctionReference Virtual::constructor;
Napi::Object Virtual::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(
env,
"_Virtual",
{
InstanceMethod("getPos", &Virtual::getMousePos),
InstanceMethod("toggleMb", &Virtual::toggleMb),
InstanceMethod("move", &Virtual::move),
InstanceMethod("scrollWheel", &Virtual::scrollWheel),
InstanceMethod("toggleKey", &Virtual::toggleKey),
InstanceMethod("printChar", &Virtual::printChar),
InstanceMethod("setForeground", &Virtual::setForeground),
InstanceMethod("isForeground", &Virtual::isForeground),
InstanceMethod("isOpen", &Virtual::isOpen),
InstanceMethod("capture", &Virtual::capture),
InstanceMethod("getColor", &Virtual::getColor),
InstanceMethod("kill", &Virtual::kill),
InstanceMethod("close", &Virtual::close),
InstanceMethod("refresh", &Virtual::refresh),
InstanceMethod("setWorkwindow", &Virtual::setWorkwindow),
InstanceMethod("getWorkwindow", &Virtual::getWorkwindow),
InstanceMethod("setView", &Virtual::setWindowView),
InstanceMethod("getView", &Virtual::getWindowView),
InstanceAccessor("lastCoords", &Virtual::getLastCoords, NULL),
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("_Virtual", func);
return exports;
}