-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_inputs.c
More file actions
157 lines (126 loc) · 4.37 KB
/
send_inputs.c
File metadata and controls
157 lines (126 loc) · 4.37 KB
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
#include "send_inputs.h"
// Keyboard
void sendChar(wchar_t character){
INPUT inputs[2] = {};
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wScan = character;
inputs[0].ki.dwFlags = KEYEVENTF_UNICODE;
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki.wScan = character;
inputs[1].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_UNICODE;
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
if (uSent != ARRAYSIZE(inputs))
{
printf("SendInput failed: 0x%x\n", HRESULT_FROM_WIN32(GetLastError()));
}
}
void sendKey(WORD keycode){
INPUT inputs[1] = {};
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = keycode;
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
if (uSent != ARRAYSIZE(inputs))
{
printf("SendInput failed: 0x%x\n", HRESULT_FROM_WIN32(GetLastError()));
}
}
void sendKeyEnd(WORD keycode){
INPUT inputs[1] = {};
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = keycode;
inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
if (uSent != ARRAYSIZE(inputs))
{
printf("SendInput failed: 0x%x\n", HRESULT_FROM_WIN32(GetLastError()));
}
}
void mouseTo(int x, int y){
INPUT inputs[1] = {};
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_MOUSE;
inputs[0].mi.dx = x;
inputs[0].mi.dy = y;
inputs[0].mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
if (uSent != ARRAYSIZE(inputs))
{
printf("SendInput failed: 0x%x\n", HRESULT_FROM_WIN32(GetLastError()));
}
}
void mouseMove(int x, int y){
INPUT inputs[1] = {};
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_MOUSE;
inputs[0].mi.dx = x;
inputs[0].mi.dy = y;
inputs[0].mi.dwFlags = MOUSEEVENTF_MOVE;
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
if (uSent != ARRAYSIZE(inputs))
{
printf("SendInput failed: 0x%x\n", HRESULT_FROM_WIN32(GetLastError()));
}
}
void mouseDown(int button){
INPUT inputs[1] = {};
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_MOUSE;
switch(button){
case VK_LBUTTON: inputs[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;break;
case VK_RBUTTON: inputs[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;break;
case VK_MBUTTON: inputs[0].mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN;break;
}
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
if (uSent != ARRAYSIZE(inputs))
{
printf("SendInput failed: 0x%x\n", HRESULT_FROM_WIN32(GetLastError()));
}
}
void mouseUp(int button){
INPUT inputs[1] = {};
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_MOUSE;
switch(button){
case VK_LBUTTON: inputs[0].mi.dwFlags = MOUSEEVENTF_LEFTUP;break;
case VK_RBUTTON: inputs[0].mi.dwFlags = MOUSEEVENTF_RIGHTUP;break;
case VK_MBUTTON: inputs[0].mi.dwFlags = MOUSEEVENTF_MIDDLEUP;break;
}
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
if (uSent != ARRAYSIZE(inputs))
{
printf("SendInput failed: 0x%x", HRESULT_FROM_WIN32(GetLastError()));
}
}
void mouseClick(int button){
mouseDown(button);
mouseUp(button);
}
void mouseScroll(int amountX, int amountY){
if(amountX != 0){
INPUT inputs[1] = {};
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_MOUSE;
inputs[0].mi.dwFlags = MOUSEEVENTF_HWHEEL;
inputs[0].mi.mouseData = amountX;
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
if (uSent != ARRAYSIZE(inputs))
{
printf("SendInput failed: 0x%x", HRESULT_FROM_WIN32(GetLastError()));
}
}
if(amountY != 0){
INPUT inputs[1] = {};
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_MOUSE;
inputs[0].mi.dwFlags = MOUSEEVENTF_WHEEL;
inputs[0].mi.mouseData = amountY;
UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
if (uSent != ARRAYSIZE(inputs))
{
printf("SendInput failed: 0x%x", HRESULT_FROM_WIN32(GetLastError()));
}
}
}