/
KeyDaemon.cpp
executable file
·319 lines (270 loc) · 8.21 KB
/
KeyDaemon.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
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
#include <process.h>
#define SC_ESC 0x01
#define SC_Q 0x10
#define SC_W 0x11
#define SC_E 0x12
#define SC_A 0x1E
#define SC_S 0x1F
#define SC_D 0x20
#define VK_Q 'q'
#define VK_W 'w'
#define VK_E 'e'
#define VK_A 'a'
#define VK_S 's'
#define VK_D 'd'
#define SC_NUMPADDEL 0x53
#define SC_NUMPADINS 0x52
#define SC_NUMPADEND 0x4F
#define SC_NUMPADHOME 0x47
#define SC_NUMPADCLEAR 0x4C
#define SC_NUMPADUP 0x48
#define SC_NUMPADDOWN 0x50
#define SC_NUMPADLEFT 0x4B
#define SC_NUMPADRIGHT 0x4D
#define SC_NUMPADPGUP 0x49
#define SC_NUMPADPGDN 0x51
#define SC_NUMPADDOT SC_NUMPADDEL
#define SC_NUMPAD0 SC_NUMPADINS
#define SC_NUMPAD1 SC_NUMPADEND
#define SC_NUMPAD2 SC_NUMPADDOWN
#define SC_NUMPAD3 SC_NUMPADPGDN
#define SC_NUMPAD4 SC_NUMPADLEFT
#define SC_NUMPAD5 SC_NUMPADCLEAR
#define SC_NUMPAD6 SC_NUMPADRIGHT
#define SC_NUMPAD7 SC_NUMPADHOME
#define SC_NUMPAD8 SC_NUMPADUP
#define SC_NUMPAD9 SC_NUMPADPGUP
#undef assert
void assert(bool cond, const char *error) {
if (cond) return;
printf("Assert failed: ");
printf(error);
printf("\n");
exit(1);
}
void ReadScreen(int x, int y, int width, int height, unsigned char *buffer) {
HDC hdc = GetDC(NULL);
HDC sdc = NULL;
HBITMAP hbitmap_screen = NULL;
HGDIOBJ sdc_orig_select = NULL;
BITMAPINFO bmapinfo;
// Create an empty bitmap to hold all the pixels currently visible on the screen (within the search area):
assert(sdc = CreateCompatibleDC(hdc), "sdc");
assert(hbitmap_screen = CreateCompatibleBitmap(hdc, width, height), "hbitmap_screen");
assert(sdc_orig_select = SelectObject(sdc, hbitmap_screen), "sdc_orig_select");
// Copy the pixels in the search-area of the screen into the DC to be searched:
assert(BitBlt(sdc, 0, 0, width, height, hdc, x, y, SRCCOPY), "BitBlt");
// get at the bits
bmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmapinfo.bmiHeader.biBitCount = 32;
bmapinfo.bmiHeader.biHeight = -height;
bmapinfo.bmiHeader.biWidth = width;
bmapinfo.bmiHeader.biPlanes = 1;
bmapinfo.bmiHeader.biCompression = BI_RGB;
GetDIBits(sdc, hbitmap_screen, 0, height, buffer, &bmapinfo, DIB_RGB_COLORS);
// clean up
SelectObject(sdc, sdc_orig_select);
DeleteDC(sdc);
DeleteObject(hbitmap_screen);
ReleaseDC(NULL, hdc);
fflush(stdout);
}
void SendEsc() {
HWND window = NULL;
DWORD myThreadId, windowThreadId;
for (int i = 0; i < 10; i++) {
window = GetForegroundWindow();
if (window) break;
Sleep(10*i + 10);
}
if (!window) {
printf("Couldn't get foreground window\n");
fflush(stdout);
return;
}
//assert(myThreadId = GetCurrentThreadId(), "myThreadId");
//assert(windowThreadId = GetWindowThreadProcessId(window, NULL), "windowThreadId");
// attach to the thread associated to the current window
//assert(AttachThreadInput(myThreadId, windowThreadId, TRUE), "attach to foreground window's input thread");
// get the keyboard state
//unsigned char keyboard[256];
//assert(GetKeyboardState(keyboard), "GetKeyboardState");
// print it
//for (int i = 0; i < 256; i++) {
//printf("%i ", keyboard[i]);
//}
//printf("\n");
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_ESCAPE;
input.ki.wScan = SC_ESC;
input.ki.dwFlags = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
SendInput(1, &input, sizeof(INPUT));
Sleep(100);
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
// detach the thread
//assert(AttachThreadInput(myThreadId, windowThreadId, FALSE), "detach from foreground window's input thread");
}
void SendPan() {
HWND window = NULL;
DWORD myThreadId, windowThreadId;
for (int i = 0; i < 10; i++) {
window = GetForegroundWindow();
if (window) break;
Sleep(10*i + 10);
}
if (!window) {
printf("Couldn't get foreground window\n");
fflush(stdout);
return;
}
//assert(myThreadId = GetCurrentThreadId(), "myThreadId");
//assert(windowThreadId = GetWindowThreadProcessId(window, NULL), "windowThreadId");
// attach to the thread associated to the current window
//assert(AttachThreadInput(myThreadId, windowThreadId, TRUE), "attach to foreground window's input thread");
// get the keyboard state
//unsigned char keyboard[256];
//assert(GetKeyboardState(keyboard), "GetKeyboardState");
// print it
//for (int i = 0; i < 256; i++) {
//printf("%i ", keyboard[i]);
//}
//printf("\n");
INPUT input;
if (rand()<RAND_MAX/2) {
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_Q;
input.ki.wScan = SC_Q;
input.ki.dwFlags = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
SendInput(1, &input, sizeof(INPUT));
Sleep(3000);
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
}
input.ki.wVk = VK_W;
input.ki.wScan = SC_W;
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(INPUT));
Sleep(2000);
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
input.ki.wVk = VK_D;
input.ki.wScan = SC_D;
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(INPUT));
Sleep(3000);
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
if (rand()<RAND_MAX/2) {
input.ki.wVk = VK_E;
input.ki.wScan = SC_E;
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(INPUT));
Sleep(6000);
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
}
// detach the thread
//assert(AttachThreadInput(myThreadId, windowThreadId, FALSE), "detach from foreground window's input thread");
}
int nearl (int a,int b) {
return a==b;
}
int iswhite(unsigned char * a) {
int r=a[0];
int g=a[1];
int b=a[2];
if ((!nearl(r,g))||!nearl(g,b)) {
return false;
}
if (r<220) return false;
return true;
}
bool issame(unsigned char *a, unsigned char *b) {
int r=(a[0]>b[0]?a[0]-b[0]:b[0]-a[0]);
int g=(a[1]>b[1]?a[1]-b[1]:b[1]-a[1]);
int bb=(a[2]>b[2]?a[2]-b[2]:b[2]-a[2]);
if (r<20||g<20||bb<20) return true;
return false;
}
int pcmp(unsigned char * newbuffer, unsigned char* oldbuffer,unsigned int numpix) {
for (unsigned int i=0;i<numpix;++i) {
if (!issame(newbuffer+i*4,oldbuffer+i*4)) {//iswhite(newbuffer+i*4)!=iswhite(oldbuffer+i*4)) {
return 1;
}
}
return 0;
}
int main(int argc, const char **argv) {
unsigned int width=1024;
unsigned int height=768;
unsigned char *buffer=(unsigned char*)malloc(width*height*4);
unsigned char *oldbuffer=(unsigned char*)malloc(width*height*4);
bool lastsame=false;
while (1) {
//printf("Looping\n");
//Sleep(6500);
// spy
ReadScreen(0, 0, width, height, buffer);
bool same=false;
if (!pcmp(buffer,oldbuffer,width*height)) {
FILE * fp=fopen("YAY.txt","a");
fprintf (fp,"Same\n");
same=true;
fclose(fp);
Sleep(6500);
if (lastsame) {
//SendEsc();
spawnl(P_WAIT,"escrestart.exe","escrestart.exe",NULL);
}
lastsame=true;
}else {
FILE * fp=fopen("YAY.txt","a");
fprintf (fp,"Diff\n");
fclose(fp);
//SendPan();
lastsame=false;
}
unsigned char *tmpbuffer=oldbuffer;
oldbuffer=buffer;
buffer=tmpbuffer;
char data[102];
static int count=0;
if (!same) {
sprintf(data,"output%d.ppm",count++);
}else {
sprintf(data,"output%dX.ppm",count++);
}
#if 0
FILE * fp = fopen(data,"wb");
char* p6head="P6\n1024 768 255\n";
fwrite(p6head,1,strlen(p6head),fp);
for (unsigned int i=0;i<height*width;++i) {
if (iswhite(&buffer[i*4]) ) {
unsigned char tmp=0xff;
fwrite(&tmp,1,1,fp);
fwrite(&tmp,1,1,fp);
fwrite(&tmp,1,1,fp);
}else {
unsigned char tmp=0x0;
fwrite(&tmp,1,1,fp);
fwrite(&tmp,1,1,fp);
fwrite(&tmp,1,1,fp);
}
}
fclose(fp);
#endif
//SendKey(VK_NUMPAD0);
printf("DONE DONE DONE\n");
//break;
}
}