-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendkey.h
49 lines (43 loc) · 1.33 KB
/
sendkey.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
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
static void SetKeyState(const BOOL bState, const WORD keyCode) {
BYTE keyState[256];
INPUT input;
ZeroMemory(&input, sizeof(input));
GetKeyboardState((LPBYTE)&keyState);
if ((bState && !(keyState[keyCode] & 1)) ||
(!bState && (keyState[keyCode] & 1))) {
input.type = INPUT_KEYBOARD;
input.ki.wVk = keyCode;
SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
}
static void SendSingleKey(const WORD keyCode) {
INPUT input;
ZeroMemory(&input, sizeof(input));
input.type = INPUT_KEYBOARD;
input.ki.wVk = keyCode;
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
static void SendMultipleKey(const WORD keyCode[], const unsigned short count) {
INPUT input;
ZeroMemory(&input, sizeof(input));
input.type = INPUT_KEYBOARD;
input.ki.dwFlags = 0;
for (unsigned short i = 0; i < count; i++) {
input.ki.wVk = keyCode[i];
SendInput(1, &input, sizeof(input));
}
input.ki.dwFlags = KEYEVENTF_KEYUP;
for (unsigned short i = count; i-- > 0;) {
input.ki.wVk = keyCode[i];
SendInput(1, &input, sizeof(input));
}
}