-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcautogui.h
264 lines (198 loc) · 4.72 KB
/
cautogui.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
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
#ifndef C_AUTOGUI_H
#define C_AUTOGUI_H
#include <windows.h>
enum MouseButtons {
MBS_LEFT,
MBS_RIGHT,
MBS_MIDDLE
};
enum Keys {
F1 = 0x70, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
K0 = 0x30, K1, K2, K3, K4, K5, K6, K7, K8, K9,
A = 0x41, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
NUMPAD0 = 0X60, NUMPAD1, NUMPAD2, NUMPAD3, NUMPAD4, NUMPAD5, NUMPAD6,
NUMPAD7, NUMPAD8, NUMPAD9,
SPACE = 0X20, END = 0X23, HOME, LEFT, UP, RIGHT, DOWN,
LSHIFT = 0XA0, RSHIFT, LCTRL, RCTRL, LALT, RALT,
LWIN = 0X5B, RWIN, MENU,
PAGEUP = 0x21, PAGEDOWN,
INSERT = 0x2D, DEL,
TAB = 0x09, ESC = 0x1B, ENTER = 0x0D, BACKSPACE = 0x08,
PRINTSCREEN = 0x2A, SCROLLLOCK = 0x91, PAUSE = 0x13
};
POINT size();
POINT position();
int onScreen(int x, int y);
void move(int x, int y);
void moveTo(int x, int y);
void scroll(int s);
void click();
void rightClick();
void mouseDown(enum MouseButtons button);
void mouseUp(enum MouseButtons button);
void drag(int x, int y, enum MouseButtons button);
void dragTo(int x, int y, enum MouseButtons button);
void write(wchar_t *str);
void writeEach(wchar_t* str, int time);
void keyDown(enum Keys key);
void keyUp(enum Keys key);
void press(enum Keys key);
void hotkey(size_t count, ...);
#endif /* C_AUTOGUI_H */
#ifdef C_AUTOGUI_IMPLEMENTATION
int counter(wchar_t* str) {
int i = 0;
while (*str) {
if (*str == ',') i++;
str++;
}
return i+1;
}
POINT size() {
POINT s;
s.x = GetSystemMetrics(SM_CXSCREEN);
s.y = GetSystemMetrics(SM_CXSCREEN);
return s;
}
POINT position() {
POINT pos;
GetCursorPos(&pos);
return pos;
}
int onScreen(int x, int y) {
POINT screen;
if (x < 0) return 0;
if (y < 0) return 0;
screen = size();
if (x > screen.x) return 0;
if (y > screen.y) return 0;
return 1;
}
void move(int x, int y) {
POINT pos;
GetCursorPos(&pos);
SetCursorPos(pos.x + x, pos.y + y);
}
void moveTo(int x, int y) {
SetCursorPos(x, y);
}
void scroll(int s) {
INPUT ip = {0};
ip.type = INPUT_MOUSE;
ip.mi.mouseData = WHEEL_DELTA * s;
ip.mi.dwFlags = MOUSEEVENTF_WHEEL;
SendInput(1, &ip, sizeof(INPUT));
}
void click() {
INPUT ip = {0};
ip.type = INPUT_MOUSE;
ip.mi.dwFlags = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
SendInput(1, &ip, sizeof(INPUT));
}
void rightClick() {
INPUT ip = {0};
ip.type = INPUT_MOUSE;
ip.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP;
SendInput(1, &ip, sizeof(INPUT));
}
void middleClick() {
INPUT ip = {0};
ip.type = INPUT_MOUSE;
ip.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN | MOUSEEVENTF_MIDDLEUP;
SendInput(1, &ip, sizeof(INPUT));
}
void mouseDown(enum MouseButtons button) {
INPUT ip = {0};
ip.type = INPUT_MOUSE;
switch(button) {
case MBS_LEFT:
ip.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; break;
case MBS_RIGHT:
ip.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN; break;
case MBS_MIDDLE:
ip.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN; break;
default:
return;
}
SendInput(1, &ip, sizeof(INPUT));
}
void mouseUp(enum MouseButtons button) {
INPUT ip = {0};
ip.type = INPUT_MOUSE;
switch(button) {
case MBS_LEFT:
ip.mi.dwFlags = MOUSEEVENTF_LEFTUP; break;
case MBS_RIGHT:
ip.mi.dwFlags = MOUSEEVENTF_RIGHTUP; break;
case MBS_MIDDLE:
ip.mi.dwFlags = MOUSEEVENTF_MIDDLEUP; break;
default:
return;
}
SendInput(1, &ip, sizeof(INPUT));
}
void drag(int x, int y, enum MouseButtons button) {
mouseDown(button);
move(x, y);
Sleep(100);
mouseUp(button);
}
void dragTo(int x, int y, enum MouseButtons button) {
mouseDown(button);
moveTo(x, y);
Sleep(100);
mouseUp(button);
}
void write(wchar_t *str) {
size_t i, length;
INPUT ip = {0};
length = wcslen(str);
for (i=0; i<length; i++) {
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = str[i];
ip.ki.dwFlags = KEYEVENTF_UNICODE;
SendInput(1, &ip, sizeof(INPUT));
}
}
void writeEach(wchar_t *str, int time) {
size_t i, length;
INPUT ip = {0};
length = wcslen(str);
for (i=0; i<length; i++) {
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = str[i];
ip.ki.dwFlags = KEYEVENTF_UNICODE;
SendInput(1, &ip, sizeof(INPUT));
Sleep(time);
}
}
void keyDown(enum Keys key) {
INPUT ip = {0};
ip.type = INPUT_KEYBOARD;
ip.ki.wVk = key;
SendInput(1, &ip, sizeof(INPUT));
}
void keyUp(enum Keys key) {
INPUT ip = {0};
ip.type = INPUT_KEYBOARD;
ip.ki.wVk = key;
ip.ki.dwFlags |= KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}
void press(enum Keys key) {
keyDown(key);
keyUp(key);
}
void hotkey(size_t count, ...) {
size_t i;
va_list args;
va_start(args, count);
for (i=0; i<count; i++)
keyDown(va_arg(args, int));
va_end(args);
va_start(args, count);
for (i=0; i<count; i++)
keyUp(va_arg(args, int));
}
#define hotkey(...) hotkey(counter(L###__VA_ARGS__), __VA_ARGS__)
#endif /* C_AUTOGUI_IMPLEMENTATION */