forked from krogank9/WifiMouseServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakeinput-windows.cpp
executable file
·470 lines (410 loc) · 15.3 KB
/
fakeinput-windows.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Because the SendInput function is only supported in
// Windows 2000 and later, WINVER needs to be set as
// follows so that SendInput gets defined when windows.h
// is included below.
#define WINVER 0x0500
#include <Windows.h>
#include <pdh.h>
#include "tchar.h"
#include <QDateTime>
#include <QString>
#include <QTimer>
#include <QDebug>
#include <QProcess>
#include <QFuture>
#include <QtConcurrent/QtConcurrent>
#include <QStandardPaths>
#include <QDir>
#include <QList>
#include "fakeinput.h"
#include "stdlib.h"
QMap<QString, WORD> virtualKeyList {
{"Return", VK_RETURN}, {"BackSpace", VK_BACK}, {"Ctrl", VK_CONTROL},
{"Win", VK_LWIN}, {"Tab", VK_TAB}, {"Alt", VK_MENU}, {"Esc", VK_ESCAPE},
{"Shift", VK_SHIFT}, {"Menu", VK_RMENU}, {"Insert", VK_INSERT},
{"Home", VK_HOME}, {"End", VK_END}, {"Space", VK_SPACE},
{"VolumeUp", VK_VOLUME_UP}, {"VolumeDown", VK_VOLUME_DOWN}, {"VolumeMute", VK_VOLUME_MUTE},
{"PauseSong", VK_MEDIA_PLAY_PAUSE}, {"NextSong", VK_MEDIA_NEXT_TRACK}, {"PrevSong", VK_MEDIA_PREV_TRACK},
{"Left", VK_LEFT}, {"Right", VK_RIGHT}, {"Up", VK_UP}, {"Down", VK_DOWN},
{"PgU", VK_PRIOR}, {"PgD", VK_NEXT},
{"F1", VK_F1}, {"F2", VK_F2}, {"F3", VK_F3}, {"F4", VK_F4}, {"F5", VK_F5}, {"F6", VK_F6},
{"F7", VK_F7}, {"F8", VK_F8}, {"F9", VK_F9}, {"F10", VK_F10}, {"F11", VK_F11}, {"F12", VK_F12},
{"0", 0x30}, {"1", 0x31}, {"2", 0x32}, {"3", 0x33}, {"4", 0x34}, {"5", 0x35},
{"6", 0x36}, {"7", 0x37}, {"8", 0x38}, {"9", 0x39},
{"a", 0x41}, {"b", 0x42}, {"c", 0x43}, {"d", 0x44}, {"e", 0x45}, {"f", 0x46},
{"g", 0x47}, {"h", 0x48}, {"i", 0x49}, {"j", 0x4a}, {"k", 0x4b}, {"l", 0x4c},
{"m", 0x4d}, {"n", 0x4e}, {"o", 0x4f}, {"p", 0x50}, {"q", 0x51}, {"r", 0x52},
{"s", 0x53}, {"t", 0x54}, {"u", 0x55}, {"v", 0x56}, {"w", 0x57}, {"x", 0x58},
{"y", 0x59}, {"z", 0x5a}, {" ", VK_SPACE}
};
QList<QFileInfo> recurseAndGetShortcutList(QDir dir, int recursions = 0) {
QList<QFileInfo> lnkList;
QFileInfoList dirInfoList = dir.entryInfoList();
for(int i=0; i<dirInfoList.length(); i++) {
QFileInfo info = dirInfoList.at(i);
if(info.isDir()) {
QDir recurDir(info.absoluteFilePath());
if(recurDir.absolutePath() != dir.absolutePath()
&& recurDir.absolutePath().startsWith(dir.absolutePath())
// don't search nested start menu folders
&& !recurDir.absolutePath().contains(QRegExp("/Roaming/Microsoft/Windows/Start Menu/Programs/.*/.*/"))) {
lnkList.append(recurseAndGetShortcutList(recurDir, recursions+1));
}
}
else if(info.isFile()) {
if(info.suffix() == "lnk"
&& !info.absoluteFilePath().contains("/Roaming/Microsoft/Windows/Recent/")
&& !info.absoluteFilePath().contains("/Roaming/Microsoft/Windows/SendTo/"))
lnkList.append(info);
}
}
return lnkList;
}
QString programsList = "";
QString getProgramsList() {
QDir dir(QDir::home().absolutePath() + "/AppData/Roaming");
QList<QFileInfo> exes = recurseAndGetShortcutList(dir);
QString programsList;
for(int i=0; i<exes.size(); i++) {
if(i != 0)
programsList += "\n";
programsList += exes.at(i).absoluteFilePath();
}
return programsList;
}
namespace FakeInput {
QString getOsName() { return "windows"; }
void platformIndependentSleepMs(qint64 ms) {
Sleep(ms);
}
QList<QString> nonBlockingCommands = {
"echo", "netstat", "ipconfig", "dir", "chdir",
"at", "fsutil", "ping", "reg", "set", "sort",
"systeminfo", "taskkill"
};
QString runCommandForResult(QString command) {
QProcess cmd;
// trick to start process detached & read output not possible on windows
// so only allow any we know that will not block for more than few hundred ms to return
for(int i=0; i<nonBlockingCommands.size(); i++) {
if(command.startsWith(nonBlockingCommands.at(i))) {
cmd.start(command);
cmd.waitForFinished(350);
QString result = cmd.readAllStandardOutput() + cmd.readAllStandardError();
qInfo() << result;
return result;
}
}
cmd.startDetached(command);
return "";
}
void sendUnicodeEvent(DWORD flags, WORD scan)
{
INPUT ip;
ZeroMemory(&ip, sizeof(ip));
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
ip.ki.dwFlags = flags;
ip.ki.wScan = scan;
ip.ki.wVk = 0;
ip.ki.dwExtraInfo = 0;
SendInput(1, &ip, sizeof(INPUT));
}
void sendSpecialKeyEvent(DWORD flags, WORD key)
{
INPUT ip;
ZeroMemory(&ip, sizeof(ip));
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
ip.ki.dwFlags = flags;
ip.ki.wScan = MapVirtualKey(key, MAPVK_VK_TO_VSC);
ip.ki.wVk = key;
ip.ki.dwExtraInfo = 0;
SendInput(1, &ip, sizeof(INPUT));
}
void sendMouseEvent(DWORD flags, LONG dx, LONG dy, DWORD mouseData)
{
INPUT input;
ZeroMemory(&input, sizeof(input));
input.type = INPUT_MOUSE;
input.mi.mouseData = mouseData;
input.mi.dx = dx;
input.mi.dy = dy;
input.mi.dwFlags = flags;
SendInput(1, &input, sizeof(input));
}
void sendMouseEventAbsolute(DWORD flags, LONG x, LONG y, DWORD mouseData)
{
INPUT input;
ZeroMemory(&input, sizeof(input));
input.type = INPUT_MOUSE;
input.mi.mouseData = mouseData;
input.mi.dx = x * (65536 / GetSystemMetrics(SM_CXSCREEN));
input.mi.dy = y * (65536 / GetSystemMetrics(SM_CYSCREEN));
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | flags;
SendInput(1, &input, sizeof(input));
}
// SendInput() function doesn't repeat keys, here's a timer to simulate
// the normal behavior in windows: keys repeating as you hold them down.
QString lastKeyDown;
qint64 lastKeyTime = 0;
bool lastKeyStillDown = false;
void keyRepeatCallback() {
if(lastKeyStillDown && QDateTime::currentMSecsSinceEpoch() - lastKeyTime > 500)
keyDown(lastKeyDown);
}
static PDH_HQUERY cpuQuery;
static PDH_HCOUNTER cpuTotal;
QTimer *keyRepeaterTimer;
void initFakeInput() {
keyRepeaterTimer = new QTimer();
keyRepeaterTimer->setSingleShot(false);
keyRepeaterTimer->setInterval(35);
keyRepeaterTimer->start();
QObject::connect(keyRepeaterTimer, &QTimer::timeout, keyRepeatCallback);
PdhOpenQuery(NULL, NULL, &cpuQuery);
// You can also use L"\\Processor(*)\\% Processor Time" and get individual CPU values with PdhGetFormattedCounterArray()
//PdhAddEnglishCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal); // old, changed for mingw
PdhAddCounterW(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
PdhCollectQueryData(cpuQuery);
programsList = getProgramsList();
}
void freeFakeInput() {
keyRepeaterTimer->stop();
delete keyRepeaterTimer;
}
void typeUnicodeChar(ushort c)
{
if(virtualKeyList.contains(QString(c))) {
keyDown(QString(c)); // This allows for key combos. ctrl+a ctrl+l
keyUp(QString(c)); // else it doesn't get registered when you use unicode scan key. need VK_ key
}
else if(virtualKeyList.contains(QString(c).toLower())) {
keyDown("Shift");
keyDown(QString(c).toLower()); // allows to use shiftkey on mobile keyboard for combos
keyUp(QString(c).toLower()); // ex. ctrl + N(capital) will open incognito in chrome
keyUp("Shift");
}
else {
sendUnicodeEvent(KEYEVENTF_UNICODE, c);
sendUnicodeEvent(KEYEVENTF_UNICODE | KEYEVENTF_KEYUP, c);
}
}
void typeChar(QChar c) {
if(c == '\n') {
keyTap("Return");
}
else {
QString hex;
hex.setNum(c.unicode(), 16);
//qInfo() << "char: " << hex << "\n";
typeUnicodeChar(c.unicode());
}
}
void typeString(QString string) {
int i = 0;
while(i < string.length()) {
typeChar(string[i++]);
}
}
void keyTap(QString key) {
keyDown(key);
keyUp(key);
}
void keyDown(QString key) {
lastKeyDown = key;
// don't repeat modifiers
if(key != "Win" && key != "Alt" && key != "Ctrl" && key != "Esc" && key != "Shift")
lastKeyStillDown = true;
lastKeyTime = QDateTime::currentMSecsSinceEpoch();
if(virtualKeyList.contains(key))
sendSpecialKeyEvent(0, virtualKeyList.value(key));
else if(virtualKeyList.contains(key.toLower())) {
keyDown("Shift");
sendSpecialKeyEvent(0, virtualKeyList.value(key.toLower()));
keyUp("Shift");
}
else if(key.length() == 1)
sendUnicodeEvent(KEYEVENTF_UNICODE, key.at(0).unicode());
}
void changeBrightness(int change) {
// this command takes a second to run. just save brightness after getting it once:
static int savedBrightness = -1;
if(savedBrightness == -1) {
QString getCmd = "powershell.exe \"(Get-WmiObject -Namespace root\\wmi -Class WmiMonitorBrightness).CurrentBrightness\"";
QProcess pShellForResult;
pShellForResult.start(getCmd);
pShellForResult.waitForFinished(5000);
QString result = pShellForResult.readAllStandardOutput();
savedBrightness = result.simplified().toInt();
}
savedBrightness += change;
savedBrightness = savedBrightness > 100 ? 100 : (savedBrightness < 0 ? 0 : savedBrightness);
QString setCmd = "powershell.exe \"(Get-WmiObject -Namespace root\\wmi -Class WmiMonitorBrightnessMethods).wmisetbrightness(0, "+QString::number(savedBrightness)+")\"";
QProcess pShell;
pShell.startDetached(setCmd);
}
void keyUp(QString key) {
lastKeyStillDown = false;
if(virtualKeyList.contains(key))
sendSpecialKeyEvent(KEYEVENTF_KEYUP, virtualKeyList.value(key));
else if(virtualKeyList.contains(key.toLower()))
sendSpecialKeyEvent(KEYEVENTF_KEYUP, virtualKeyList.value(key.toLower()));
else if(key == "BrightnessUp")
changeBrightness(10);
else if(key == "BrightnessDown")
changeBrightness(-10);
else if(key.length() > 0)
sendUnicodeEvent(KEYEVENTF_UNICODE | KEYEVENTF_KEYUP, key.at(0).unicode());
}
void mouseSetPos(int x, int y) {
sendMouseEventAbsolute(MOUSEEVENTF_MOVE, x, y, 0);
}
void mouseMove(int addX, int addY) {
sendMouseEvent(MOUSEEVENTF_MOVE, addX, addY, 0);
}
DWORD buttonToFlags(int button, bool down) {
if(button == 1) {
if(down)
return MOUSEEVENTF_LEFTDOWN;
else
return MOUSEEVENTF_LEFTUP;
}
else if(button == 2) {
if(down)
return MOUSEEVENTF_MIDDLEDOWN;
else
return MOUSEEVENTF_MIDDLEUP;
}
else {//if button == 3
if(down)
return MOUSEEVENTF_RIGHTDOWN;
else
return MOUSEEVENTF_RIGHTUP;
}
}
void mouseDown(int button) {
sendMouseEvent(buttonToFlags(button, true), 0, 0, 0);
//platformIndependentSleepMs(10); // sleep for good measure
}
void mouseUp(int button) {
sendMouseEvent(buttonToFlags(button, false), 0, 0, 0);
}
void mouseScroll(int amount) {
sendMouseEvent(MOUSEEVENTF_WHEEL, 0, 0, amount*-100);
}
void shutdown() {
runCommandForResult("shutdown -t 0");
}
void restart() {
runCommandForResult("shutdown -t 0 -r -f");
}
void logout() {
runCommandForResult("shutdown -t 0 -l");
}
void sleep() {
runCommandForResult("psshutdown -t 0 -d");
}
void lock_screen() {
LockWorkStation();
}
void blank_screen() {
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
}
QString getCommandSuggestions(QString command)
{
return "assoc\nat\nattrib\nbootcfg\ncd\nchkdsk\ncls\ncopy\ndel\ndir\ndiskpart\ndriverquery\necho\nexit\nfc\nfind\nfindstr\nfor\nfsutil\nftype\ngetmac\ngoto\nif\nipconfig\nmd\nmore\nmove\nnet\nnetsh\nnetstat\npath\npathping\npause\nping\npopd\npowercfg\nreg\nrmdir\nren\nsc\nschtasks\nset\nsfc\nshutdown\nsort\nstart\nsubst\nsysteminfo\ntaskkill\ntasklist\ntree\ntype\nvssadmin\nxcopy";
}
QString getApplicationNames() {
QStringList sorted = programsList.split("\n");
// Remove file extensions & prefix
for(int i=0; i<sorted.size(); i++) {
QString cur = sorted.at(i);
cur = cur.mid(cur.lastIndexOf("/")+1);
cur = cur.mid(0, cur.indexOf("."));
sorted[i] = cur;
}
// Remove uninstall program shortcuts
for(int i=0; i<sorted.size(); i++)
if(sorted.at(i).startsWith("Uninstall "))
sorted.removeAt(i--);
// Sort alphabetically
for(int i=0; i<sorted.size()-1; i++)
for(int j=i+1; j<sorted.size(); j++)
if(sorted.at(i) > sorted.at(j))
sorted.swap(i, j);
return sorted.join("\n");
}
void startApplicationByName(QString name) {
QStringList apps = programsList.split("\n");
for(int i=0; i<apps.size(); i++) {
QString appName = apps.at(i);
if(appName.endsWith(name+".lnk")) {
std::wstring wstr = appName.toStdWString();
ShellExecute(NULL, NULL, wstr.data(), NULL, NULL, SW_NORMAL);
break;
}
}
}
QString getCpuUsage() {
PDH_FMT_COUNTERVALUE counterVal;
PdhCollectQueryData(cpuQuery);
PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal);
return QString::number((int)counterVal.doubleValue);
}
unsigned long lastTotalRamKbs = 8*1000*1000;
QString getRamUsage() {
MEMORYSTATUSEX statex;
ZeroMemory(&statex, sizeof(statex));
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
unsigned long total = statex.ullTotalPhys;
unsigned long available = statex.ullAvailPhys;
unsigned long used = total - available;
lastTotalRamKbs = total > 0 ? total/1000 : 8000000;
return QString::number(used/1000) + " " + QString::number(total/1000);
}
QString getProcessesFromPowershell() {
QProcess pShell;
// pid / cpu / mem / name
pShell.start("powershell.exe \"$perflist = (get-wmiobject Win32_PerfFormattedData_PerfProc_Process); foreach ($p in $perflist) {'' + $p.IDProcess + ' ' + $p.PercentProcessorTime + ' ' + $p.WorkingSet + ' ' + $p.name}\"");
pShell.waitForFinished();
return pShell.readAllStandardOutput();
}
QFuture<QString> pFuture;
QString lastFutureResult;
qint64 lastFutureResultTime = 0;
QString getProcesses() {
if(pFuture.isFinished() && QDateTime::currentMSecsSinceEpoch() - lastFutureResultTime > 3000) {
lastFutureResultTime = QDateTime::currentMSecsSinceEpoch();
if(pFuture.resultCount() > 0)
lastFutureResult = pFuture.result();
pFuture = QtConcurrent::run(getProcessesFromPowershell);
}
// pid / cpu / mem / name
QString perfList = lastFutureResult;
QStringList toList = perfList.split("\n");
for(int i=0; i<toList.size(); i++) {
QStringList info = toList[i].simplified().split(" ");
if(info.size() < 2)
continue;
long pMemoryKbs = info[2].toULong()/1000;
float pctMem = ((float)pMemoryKbs)/((float)lastTotalRamKbs);
int pctMemInt = (pctMem*100.0f);
info[2] = QString::number(pctMemInt);
toList[i] = info.join(" ");
}
// filter junk processes
for(int i=0; i<toList.size(); i++)
if(toList[i].endsWith("_Total") || toList[i].endsWith("Idle")
|| toList[i].contains("WifiMouseServer")
|| toList[i].contains("svchost") || toList[i].contains("powershell")
|| toList[i].contains("conhost") || toList[i].endsWith("Memory Compression"))
toList.removeAt(i--);
return toList.join("\n");
}
void killProcess(QString pid) {
QProcess killer;
killer.startDetached("Taskkill /PID "+pid.simplified()+" /F");
}
}