-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathemit-x.cpp
More file actions
executable file
·140 lines (138 loc) · 3.71 KB
/
emit-x.cpp
File metadata and controls
executable file
·140 lines (138 loc) · 3.71 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
#include "emit-x.h"
int WINAPI WinMain(HINSTANCE i, HINSTANCE p, LPSTR c, int s) {
std::vector<std::wstring> keys;
if (!translate(leaf(module()), &keys))
return -1;
return !send(keys);
}
bool translate(std::wstring s, std::vector<std::wstring>* v) {
std::wregex r(L"^emit(?:-(\\w+)){1,}\\.exe",
std::regex_constants::icase);
s = lower(s);
if (!std::regex_match(s, r))
return false;
std::wregex R(L"-(\\w+)");
std::wsregex_iterator i(s.begin(), s.end(), R), end;
for (;i != end; i++)
v->push_back((*i)[1]);
return true;
}
bool send(std::vector<std::wstring> keys) {
auto L = keys.size();
auto L2 = L * 2;
auto inputs = new INPUT[L2];
for (auto c = 0; c < L; c++){
inputs[c + L].type = inputs[c].type = INPUT_KEYBOARD;
inputs[c + L].ki.wVk = inputs[c].ki.wVk = vk(keys[c]);
inputs[c + L].ki.wScan = inputs[c].ki.wScan = 0;
inputs[c].ki.dwFlags = 0;
inputs[c + L].ki.dwFlags = KEYEVENTF_KEYUP;
inputs[c + L].ki.time = inputs[c].ki.time = 0;
inputs[c + L].ki.dwExtraInfo = inputs[c].ki.dwExtraInfo = NULL;
}
bool affected = L2 != SendInput(L2, inputs, sizeof(INPUT));
delete [] inputs;
return affected;
}
LPARAM vk(std::wstring str) {
std::wregex ctrl(L"^ctr?l$");
if (std::regex_match(str, ctrl))
return VK_CONTROL;
if (str == L"shift")
return VK_SHIFT;
if (str == L"alt")
return VK_MENU;
std::wregex win(L"^win(dows)?$");
if (std::regex_match(str, win))
return VK_LWIN;
std::wregex prtscr(L"^(prt|print)(scr|screen)$");
if (std::regex_match(str, prtscr))
return VK_SNAPSHOT;
std::wregex del(L"^del(ete)?$");
if (std::regex_match(str, del))
return VK_DELETE;
std::wregex bs(L"^(back(space)?)|bs$");
if (std::regex_match(str, bs))
return VK_BACK;
std::wregex enter(L"^ent(er)?$");
if (std::regex_match(str, enter))
return VK_RETURN;
if (str == L"tab")
return VK_TAB;
if (str == L"pageup")
return VK_PRIOR;
if (str == L"pagedown")
return VK_NEXT;
if (str == L"home")
return VK_HOME;
if (str == L"end")
return VK_END;
if (str == L"left")
return VK_LEFT;
if (str == L"right")
return VK_RIGHT;
if (str == L"up")
return VK_UP;
if (str == L"down")
return VK_DOWN;
if (str == L"escape" || str == L"esc")
return VK_ESCAPE;
if (str == L"space")
return VK_SPACE;
std::wregex fkey(L"^f([0-9]+)$");
std::wsmatch fkey_match;
if (std::regex_match(str, fkey_match, fkey)) {
auto fnr = std::stoi(fkey_match[1]);
if (fnr > 24) return NULL;
return VK_F1 + (fnr - 1);
}
if (str.length() > 1) return NULL;
TCHAR ch = str[0];
if (L'0' <= ch && ch <= L'9' || L'A' <= ch && ch <= L'Z')
return ch;
if (L'a' <= ch && ch <= L'z')
return ch - 32;
return NULL;
}
std::wstring module() {
WCHAR* b = NULL;
for (unsigned int c = 256, n = c; c < INT_MAX; c <<= 2) {
if (b) delete [] b;
b = new WCHAR[c];
n = GetModuleFileNameW(NULL, b, c);
if (c != n)
break;
}
std::wstring s(b);
delete [] b;
return s;
}
std::wstring lower(std::wstring s) {
std::transform(s.begin(), s.end(), s.begin(), towlower);
return s;
}
std::wstring env(std::wstring name) {
auto l = GetEnvironmentVariableW(name.c_str(), NULL, 0) + 1;
auto b = new WCHAR[l];
auto r = GetEnvironmentVariableW(name.c_str(), b, l);
std::wstring s(b);
delete [] b;
return s;
}
std::wstring leaf(std::wstring path) {
auto L = path.length() + 1;
auto b = new WCHAR[L];
memcpy(b, path.c_str(), sizeof(WCHAR) * L);
PathStripPathW(b);
std::wstring s(b);
delete [] b;
return s;
}
std::wstring join(std::wstring x, std::wstring y){
WCHAR* b;
auto r = PathAllocCombine(x.c_str(), y.c_str(),
PATHCCH_ALLOW_LONG_PATHS, &b);
std::wstring s(b);
LocalFree(b);
return s;
}