| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- library MouseKeyHook;
- { Important note about DLL memory management: ShareMem must be the
- first unit in your library's USES clause AND your project's (select
- Project-View Source) USES clause if your DLL exports any procedures or
- functions that pass strings as parameters or function results. This
- applies to all strings passed to and from your DLL--even those that
- are nested in records and classes. ShareMem is the interface unit to
- the BORLNDMM.DLL shared memory manager, which must be deployed along
- with your DLL. To avoid using BORLNDMM.DLL, pass string information
- using PChar or ShortString parameters. }
- uses
- Windows,
- Classes,
- Messages,
- SysUtils;
- const HOOK_MEM_FILENAME = 'MEM_FILE';
- type
- TShared = record
- LastInputTick: Cardinal;
- end;
- PShared = ^TShared;
- var
- MemFile: THandle;
- Shared: PShared;
-
- MouseHk,
- KeyHK: HHOOK;
- {$R *.res}
- //------------------------------------------------------------------------------
- function KeyHookProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
- begin
- Result := 0;
- if nCode < 0 then
- Result := CallNextHookEx(KeyHK,
- nCode,
- wParam,
- lParam)
- else
- begin
- Shared^.LastInputTick := GetTickCount;
- end;
- end;
-
- //------------------------------------------------------------------------------
- function MouseHookProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
- begin
- Result := 0;
- if nCode < 0 then
- Result := CallNextHookEx(MouseHk,
- nCode,
- wParam,
- lParam)
- else
- begin
- if (WPARAM = WM_LBUTTONDBLCLK) or
- (WPARAM = WM_RBUTTONDBLCLK) then
- begin
- Shared^.LastInputTick := GetTickCount;
- end;
- end;
- end;
- //------------------------------------------------------------------------------
- function GetLastInputTick: Cardinal; stdcall; export;
- begin
- Result := Shared^.LastInputTick;
- end;
- //------------------------------------------------------------------------------
- procedure SetLastInputTick(Value: Cardinal); stdcall; export;
- begin
- Shared^.LastInputTick := Value;
- end;
- //------------------------------------------------------------------------------
- function EnableMouseHook: Boolean; stdcall; export;
- begin
- if MouseHk = 0 then MouseHk := SetWindowsHookEx(WH_MOUSE,@MouseHookProc,HInstance,0);
- if MouseHk = 0 then
- begin
- Result := False;
- end
- else
- Result := True;
- end;
-
- //------------------------------------------------------------------------------
- function DisableMouseHook:Boolean; stdcall; export;
- begin
- Result := UnHookWindowsHookEx(MouseHk);
- end;
-
- //------------------------------------------------------------------------------
- function EnableKeyHook: Boolean; stdcall; export;
- begin
- if KeyHk = 0 then KeyHk := SetWindowsHookEx(WH_KEYBOARD,@KeyHookProc, HInstance, 0);
- if KeyHk = 0 then
- begin
- Result := False;
- end
- else
- Result := True;
- end;
-
- //------------------------------------------------------------------------------
- function DisableKeyHook:Boolean; stdcall; export;
- begin
- Result := UnHookWindowsHookEx(KeyHk);
- end;
- procedure DllEntry(dwReason : integer);
- begin
- case dwReason Of
- // DLLµ¼Èë½ø³Ìʱ
- DLL_PROCESS_ATTACH:
- begin
- MemFile := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, HOOK_MEM_FILENAME);
- if MemFile = 0 then
- MemFile := CreateFileMapping(DWORD(-1), nil,
- PAGE_READWRITE,
- 0,
- SizeOf(TShared),
- HOOK_MEM_FILENAME);
-
- Shared := MapViewOfFile(MemFile,
- FILE_MAP_ALL_ACCESS,
- 0,
- 0,
- SizeOf(TShared));
- Shared^.LastInputTick := GetTickCount;
- end;
- DLL_PROCESS_DETACH:
- begin
- UnmapViewOfFile(Shared);
- CloseHandle(memFile);
- end;
- end;
- end;
- exports
- EnableMouseHook,
- DisableMouseHook,
- EnableKeyHook,
- DisableKeyHook,
- GetLastInputTick,
- SetLastInputTick;
- begin
- DllProc := @DllEntry;
- DllEntry(DLL_PROCESS_ATTACH);
- end.
|