-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkeyboard_w32.hpp
196 lines (175 loc) · 4.3 KB
/
keyboard_w32.hpp
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
/*
* Copyright (C) 2013 George Wong
* GNU General Public License
*
* Notes:
* > There is certainly not full support for all
* punctuation at this time
*/
namespace KEYBOARD {
// Forward declarations
int lchar_press (const char c);
int char_press (const char c);
int any_char_press (const char c);
int string_write (std::string s);
int space_press ();
int num_press (uint32_t n);
int gen_press (uint32_t n, bool shift);
int gen_press (uint32_t n);
int shift_down ();
int shift_up ();
// Lowercase character (alpha) press (down & up)
int lchar_press (const char c) {
try {
INPUT Input = {0};
Input.type = INPUT_KEYBOARD;
Input.ki.wVk = toupper(c);
::SendInput(1,&Input,sizeof(INPUT));
Input.ki.dwFlags = KEYEVENTF_KEYUP;
::SendInput(1,&Input,sizeof(INPUT));
} catch (...) {
std::cout << "Unable to write character " << c << std::endl;
return 1;
}
return 0;
}
// General character (alpha) press (down & up)
int char_press (const char c) {
if (c == toupper(c)) {
shift_down();
lchar_press(c);
shift_up();
} else {
lchar_press(c);
}
return 0;
}
// Any (all typeable) character press (down & up)
int any_char_press (const char c) {
// Space
if (c==' ') {
space_press();
return 0;
}
// Number
if (c>=0x30 && c<0x40) {
num_press(c-0x30);
return 0;
}
// Alpha
if ((c>=65 && c<=90) || (c>=97 && c<=122)) {
char_press(c);
return 0;
}
// Punct (incomplete)
switch ((int)c) {
case 33:
gen_press(0x31,true);
return 0;
case 64:
gen_press(0x32,true);
return 0;
case 35:
gen_press(0x33,true);
return 0;
case 36:
gen_press(0x34,true);
return 0;
case 37:
gen_press(0x35,true);
return 0;
case 94:
gen_press(0x36,true);
return 0;
case 38:
gen_press(0x37,true);
return 0;
case 42:
gen_press(0x38,true);
return 0;
case 40:
gen_press(0x39,true);
return 0;
case 41:
gen_press(0x30,true);
return 0;
case 46:
gen_press(0xbe);
return 0;
case 44:
gen_press(0xbc);
return 0;
}
// Other
std::cout << "Unrecognized character" << std::endl;
std::cout << (int)c << " <- " << c << std::endl;
return -1;
}
// Write a string of characters
int string_write (std::string s) {
const char *cs = s.c_str();
for (size_t i=0; i<strlen(cs); i++) {
any_char_press(cs[i]);
}
std::cout << std::endl;
return 0;
}
// Space down&up
int space_press () {
INPUT Input = {0};
Input.type = INPUT_KEYBOARD;
Input.ki.wVk = VK_SPACE;
::SendInput(1,&Input,sizeof(INPUT));
Input.ki.dwFlags = KEYEVENTF_KEYUP;
::SendInput(1,&Input,sizeof(INPUT));
return 0;
}
// Number (single character) down&up
int num_press (uint32_t n) {
INPUT Input = {0};
Input.type = INPUT_KEYBOARD;
Input.ki.wVk = 0x30+n;
::SendInput(1,&Input,sizeof(INPUT));
Input.ki.dwFlags = KEYEVENTF_KEYUP;
::SendInput(1,&Input,sizeof(INPUT));
return 0;
}
// Press down key with wVk as given by `n' with or w/o `shift'
int gen_press (uint32_t n, bool shift) {
try {
if (shift) shift_down();
INPUT Input = {0};
Input.type = INPUT_KEYBOARD;
Input.ki.wVk = n;
::SendInput(1,&Input,sizeof(INPUT));
Input.ki.dwFlags = KEYEVENTF_KEYUP;
::SendInput(1,&Input,sizeof(INPUT));
if (shift) shift_up();
} catch (...) {
std::cout << "Unable to press key with wVk value " << n << std::endl;
return -1;
}
return 0;
}
// Press down key with wVk as given by `n'
int gen_press (uint32_t n) {
return gen_press(n,false);
}
// Shift down
int shift_down () {
INPUT Input = {0};
Input.type = INPUT_KEYBOARD;
Input.ki.wVk = VK_LSHIFT;
::SendInput(1,&Input,sizeof(INPUT));
return 0;
}
// Shift up
int shift_up () {
INPUT Input = {0};
Input.type = INPUT_KEYBOARD;
Input.ki.dwFlags = KEYEVENTF_KEYUP;
Input.ki.wVk = VK_LSHIFT;
::SendInput(1,&Input,sizeof(INPUT));
return 0;
}
}