-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendinput.h
159 lines (148 loc) · 5.21 KB
/
sendinput.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
#ifndef SENDINPUT_H
#define SENDINPUT_H
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "crc32.h"
#include "command.h"
extern void EmitError(unsigned short);
const LPSTR ErrorInfo[] = {
"no error",
"keyCodeCombine == -1, VkKeyScan fails",
"unknown high-order byte of VkKeyScan's return value",
"cannot get high-order byte from VkKeyScan"
};
static const unsigned char vkeyCodeCombine[3] = {
VK_SHIFT, // shift
VK_CONTROL, // ctrl
VK_MENU // alt
};
void ParseSendKeys(const char *keyString, size_t length) {
if (length < 1)
return;
for (size_t index = 0; index < length;) {
if (index & 0x200)
Sleep(1); // workaround: preventing mistype by waiting for a while
if (keyString[index] == '\r' && keyString[index + 1] == '\n') {
// skip cr-lf line endings
++index;
}
if (length - index > 4 && keyString[index] == '$' && keyString[index + 1] == '{') {
char param[3][COMMAND_MAX_LENGTH] = {{0}};
if (sscanf(keyString + index, "${%15[A-Z0-9]}", param[0]) == 1) {
const size_t offset = 3 + strnlen(param[0], COMMAND_MAX_LENGTH);
if (keyString[index + offset - 1] == '}') {
const uint32_t hash = crc32(param[0], offset - 3);
if (hash == 1005452284) { // DOLLAR
ParseSendKeys("$", 1);
index += offset;
continue;
} else {
const WORD keyCode = ParseCommand(hash);
if (keyCode) {
SendSingleKey(keyCode);
index += offset;
continue;
}
}
}
}
if (sscanf(keyString + index, "${%15[A-Z] %9[0-9]}", param[0], param[1]) == 2) {
const size_t offset = 4 + strnlen(param[0], COMMAND_MAX_LENGTH) +
strnlen(param[1], COMMAND_MAX_LENGTH);
if (keyString[index + offset - 1] == '}') {
const int paramInt = atoi(param[1]);
if (ParseCommandWithParams(
crc32(param[0], strnlen(param[0], COMMAND_MAX_LENGTH)),
¶mInt, 1)) {
index += offset;
continue;
}
}
}
if (sscanf(keyString + index, "${%15[A-Z]+%15[0-9A-Za-z]}", param[0], param[1]) == 2) {
WORD paramLength[2];
for (unsigned char i = 0; i < 2; i++)
paramLength[i] = (WORD) strnlen(param[i], COMMAND_MAX_LENGTH);
const size_t offset = 4 + paramLength[0] + paramLength[1];
if (keyString[index + offset - 1] == '}') {
if (ParseKeyCombination(param, paramLength, 2)) {
index += offset;
continue;
}
}
}
if (sscanf(keyString + index, "${%15[A-Z] %9[0-9] %9[0-9]}", param[0], param[1],
param[2]) == 3) {
const size_t offset = 5 + strnlen(param[0], COMMAND_MAX_LENGTH) +
strnlen(param[1], COMMAND_MAX_LENGTH) +
strnlen(param[2], COMMAND_MAX_LENGTH);
if (keyString[offset + index - 1] == '}') {
int paramInt[2];
paramInt[0] = atoi(param[1]);
paramInt[1] = atoi(param[2]);
if (ParseCommandWithParams(
crc32(param[0], strnlen(param[0], COMMAND_MAX_LENGTH)),
paramInt, 2)) {
index += offset;
continue;
}
}
}
if (sscanf(keyString + index, "${%15[A-Z]+%15[0-9A-Za-z]+%15[0-9A-Za-z]}",
param[0], param[1], param[2]) == 3) {
WORD paramLength[3];
for (unsigned char i = 0; i < 3; i++) {
paramLength[i] = (WORD) strnlen(param[i], COMMAND_MAX_LENGTH);
};
const size_t offset =
5 + paramLength[0] + paramLength[1] + paramLength[2];
if (keyString[offset + index - 1] == '}') {
if (ParseKeyCombination(param, paramLength, 3)) {
index += offset;
continue;
}
}
}
}
// const SHORT vKeyCode = VkKeyScanEx(keyString[0],GetKeyboardLayout(0));
const SHORT vKeyCode = VkKeyScan(keyString[index]);
WORD keyCode[3] = {0};
keyCode[0] = (WORD) (vKeyCode & 0xFF);
const WORD keyCodeCombine = (const WORD) (vKeyCode >> 8);
if (vKeyCode == -1) {
EmitError(1); // VkKeyScan fails
++index;
continue;
}
if (keyCodeCombine == 0) {
SendSingleKey(keyCode[0]);
++index;
continue;
}
unsigned char keyCount = 0;
for (unsigned char i = 0; i < 3; i++) {
if (((keyCodeCombine >> i) & 0x1) == 1) {
keyCode[keyCount + 1] = keyCode[keyCount];
keyCode[keyCount] = vkeyCodeCombine[i];
keyCount++;
}
}
if (keyCount > 0)
SendMultipleKey(keyCode, (const unsigned short) (keyCount + 1));
else {
if (keyCodeCombine > 4)
EmitError(2); // unknown high-order byte of VkKeyScan's return value
else
EmitError(3); // cannot get high-order byte from VkKeyScan
SendSingleKey(keyCode[0]);
}
++index;
}
}
#endif //SENDINPUT_H