-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDummyUI.cpp
More file actions
127 lines (113 loc) · 4.32 KB
/
DummyUI.cpp
File metadata and controls
127 lines (113 loc) · 4.32 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
// DummyUI.cpp
// Minimal console "UI": prints one key per line from \\.\pipe\KeystrokeOverlayPipe
// Build (MinGW-w64): x86_64-w64-mingw32-g++ DummyUI.cpp -o DummyUI.exe -static -lkernel32 -luser32
#include <windows.h>
#include <string>
#include <iostream>
#include <vector>
#include <cstdint>
#include <cstring>
static const wchar_t* kPipe = LR"(\\.\pipe\KeystrokeOverlayPipe)";
static bool ReadExact(HANDLE h, void* buf, DWORD len) {
auto* p = static_cast<uint8_t*>(buf);
DWORD got = 0;
while (got < len) {
DWORD n = 0;
if (!ReadFile(h, p + got, len - got, &n, nullptr) || n == 0) return false;
got += n;
}
return true;
}
// Very small extractor: for each {"t":...,"vk":...,"text":"..."} inside "events":[...]
// prints either text (if non-empty) or vk.
static void PrintOneLinePerEvent(const std::string& frame) {
const char* s = frame.c_str();
// find events array
const char* events = strstr(s, "\"events\"");
if (!events) return;
const char* p = strchr(events, '[');
if (!p) return;
++p;
int depth = 0;
std::string obj;
for (const char* it = p; *it; ++it) {
if (*it == '{') {
if (depth++ == 0) obj.clear();
obj.push_back(*it);
} else if (*it == '}') {
obj.push_back(*it);
if (--depth == 0) {
// extract "text" (string) and "vk" (number)
auto pickString = [&](const char* key)->std::string {
std::string needle = std::string("\"")+key+"\"";
size_t pos = obj.find(needle);
if (pos == std::string::npos) return {};
pos = obj.find(':', pos);
if (pos == std::string::npos) return {};
++pos;
while (pos < obj.size() && obj[pos] == ' ') ++pos;
if (pos >= obj.size() || obj[pos] != '"') return {};
size_t i = pos + 1; bool esc = false;
for (; i < obj.size(); ++i) {
char c = obj[i];
if (c == '\\' && !esc) { esc = true; continue; }
if (c == '"' && !esc) break;
esc = false;
}
return obj.substr(pos + 1, i - (pos + 1));
};
auto pickNumber = [&](const char* key)->std::string {
std::string needle = std::string("\"")+key+"\"";
size_t pos = obj.find(needle);
if (pos == std::string::npos) return {};
pos = obj.find(':', pos);
if (pos == std::string::npos) return {};
++pos;
while (pos < obj.size() && obj[pos] == ' ') ++pos;
size_t i = pos;
while (i < obj.size() && (obj[i] == '-' || obj[i] == '+' || (obj[i] >= '0' && obj[i] <= '9'))) ++i;
return obj.substr(pos, i - pos);
};
std::string text = pickString("text");
if (!text.empty())
std::cout << text << "\n";
else {
std::string vk = pickNumber("vk");
if (!vk.empty()) std::cout << "VK:" << vk << "\n";
}
}
} else {
if (depth > 0) obj.push_back(*it);
}
if (*it == ']' && depth == 0) break; // end of events array
}
}
int main() {
SetConsoleOutputCP(CP_UTF8); // show Unicode chars properly
for (;;) {
// wait/connect loop
if (!WaitNamedPipeW(kPipe, 2000)) {
Sleep(300);
continue;
}
HANDLE h = CreateFileW(kPipe, GENERIC_READ, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (h == INVALID_HANDLE_VALUE) {
Sleep(300);
continue;
}
// read frames until disconnect
for (;;) {
DWORD len = 0;
if (!ReadExact(h, &len, sizeof(len))) break;
if (len == 0 || len > 8 * 1024 * 1024) break;
std::string buf(len, '\0');
if (!ReadExact(h, buf.data(), len)) break;
// minimal per-event printing
PrintOneLinePerEvent(buf);
}
CloseHandle(h);
// reconnect
Sleep(200);
}
return 0;
}