This repository was archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMouse.cpp
96 lines (89 loc) · 2.29 KB
/
Mouse.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
#include "stdafx.h"
static void setInputWheel(INPUT* q, int d, bool h) {
q->type = INPUT_MOUSE;
q->mi.dwFlags = (h ? MOUSEEVENTF_HWHEEL : MOUSEEVENTF_WHEEL);
q->mi.mouseData = d;
q->mi.dx = 0;
q->mi.dy = 0;
q->mi.time = 0;
}
static void setInputMouseButton(INPUT* q, int mf) {
q->type = INPUT_MOUSE;
q->mi.dwFlags = mf;
q->mi.dx = 0;
q->mi.dy = 0;
q->mi.time = 0;
q->mi.mouseData = 0;
}
static int getpos(lua_State* q) {
POINT p{};
if (GetCursorPos(&p)) {
lua_pushinteger(q, p.x);
lua_pushinteger(q, p.y);
return 2;
} else return 0;
}
static int setpos(lua_State* q) {
bool ok = SetCursorPos((int)lua_tointeger(q, 1), (int)lua_tointeger(q, 2));
lua_pushboolean(q, ok);
return 1;
}
static int check(lua_State* q) {
UINT vk = 0;
switch (lua_tointeger(q, 1)) {
case 1: vk = VK_LBUTTON; break;
case 2: vk = VK_RBUTTON; break;
case 3: vk = VK_MBUTTON; break;
case 4: vk = VK_XBUTTON1; break;
case 5: vk = VK_XBUTTON2; break;
}
lua_pushboolean(q, KeyCheck(vk));
return 1;
}
// (amt, hor = false)
static int scroll(lua_State* q) {
auto amt = (int)(lua_tonumber(q, 1) * WHEEL_DELTA);
auto hwheel = lua_gettop(q) > 1 && lua_toboolean(q, 2);
INPUT input{};
setInputWheel(&input, amt, hwheel);
SendInput(1, &input, sizeof(INPUT));
return 0;
}
// (btn)
static int press(lua_State* q) {
int flag;
switch (lua_tointeger(q, 1)) {
case 1: flag = MOUSEEVENTF_LEFTDOWN; break;
case 2: flag = MOUSEEVENTF_RIGHTDOWN; break;
case 3: flag = MOUSEEVENTF_MIDDLEDOWN; break;
default: return 0;
}
INPUT input{};
setInputMouseButton(&input, flag);
SendInput(1, &input, sizeof(INPUT));
return 0;
}
// (btn)
static int release(lua_State* q) {
int flag;
switch (lua_tointeger(q, 1)) {
case 1: flag = MOUSEEVENTF_LEFTUP; break;
case 2: flag = MOUSEEVENTF_RIGHTUP; break;
case 3: flag = MOUSEEVENTF_MIDDLEUP; break;
default: return 0;
}
INPUT input{};
setInputMouseButton(&input, flag);
SendInput(1, &input, sizeof(INPUT));
return 0;
}
void init_mouse_lualib(lua_State* q) {
lua_newtable(q);
luaM_rawset_str_cfunc(q, "getpos", getpos);
luaM_rawset_str_cfunc(q, "setpos", setpos);
luaM_rawset_str_cfunc(q, "check", check);
luaM_rawset_str_cfunc(q, "press", press);
luaM_rawset_str_cfunc(q, "release", release);
luaM_rawset_str_cfunc(q, "scroll", scroll);
lua_setglobal(q, "mouse");
}