Whiler | J'ai utilisé ce code là dans le passé qui te permet de hooker le clavier mais aussi de bloquer ou non les touches si tu le souhaites via la fonction setPassthru :
Code :
- library toto;
- uses
- Windows;
- {$R *.RES}
- type THookData=record
- Handle,Msg,
- hHook,Instances:Cardinal;
- PassThru:Boolean;
- end;
- PHookData=^THookData;
- const
- MutexName='UniqueMutexName';
- MemShareName='UniqueMemShareName';
- var
- MyMutex,
- MemShare:Cardinal;
- MyData:PHookData;
- function KbdHook(hCode,wParam:LongInt;lParam:LongInt):Longint;stdcall;
- begin
- try
- WaitForSingleObject(MyMutex,INFINITE);
- if (MyData^.Msg<>0) and (hCode=HC_ACTION) then PostMessage(MyData^.Handle,MyData^.Msg,wParam,lParam);
- if (MyData^.hHook <> 0) then begin
- if(MyData^.PassThru = False) then Result:=-1
- else Result:=CallNextHookEx(MyData^.hHook,hCode,wParam,lParam)
- end else Result:=0
- finally ReleaseMutex(MyMutex);
- end;
- end;
- function hookKbd(Hwnd,MsgID:Cardinal):LongInt;stdcall;
- begin
- try
- WaitForSingleObject(MyMutex,INFINITE);
- if MyData^.hHook<>0 then begin
- Result:=0;ReleaseMutex(MyMutex);Exit;
- end;
- Result:=SetWindowsHookEx(WH_KEYBOARD,@KbdHook,HInstance,0);
- if Result<>0 then begin
- MyData^.hHook:=Result;
- MyData^.Msg:=MsgID;
- MyData^.Handle:=Hwnd;
- MyData^.PassThru:=True;
- end;
- finally ReleaseMutex(MyMutex);
- end;
- end;
- function unHookKbd:Boolean;stdcall;
- begin
- try
- WaitForSingleObject(MyMutex,INFINITE);
- Result:=True;
- if MyData^.hHook=0 then begin
- ReleaseMutex(MyMutex);Exit;
- end;
- Result:=UnhookWindowsHookEx(MyData^.hHook);
- if Result=True then begin
- MyData^.hHook:=0; MyData^.Msg:=0; MyData^.Handle:=0; MyData^.PassThru:=True;
- end;
- finally ReleaseMutex(MyMutex);
- end;
- end;
- function isHooked:Boolean;stdcall;
- begin
- WaitForSingleObject(MyMutex,INFINITE);
- Result:=(MyData^.hHook<>0);
- ReleaseMutex(MyMutex);
- end;
- function setPassthru(NewPassthru:Boolean):Boolean;stdcall;
- begin
- WaitForSingleObject(MyMutex,INFINITE);
- Result:=MyData^.Passthru;
- MyData^.Passthru:=NewPassthru;
- ReleaseMutex(MyMutex);
- end;
- function getPassthru:Boolean;stdcall;
- begin
- WaitForSingleObject(MyMutex,INFINITE);
- Result:=MyData^.Passthru;
- ReleaseMutex(MyMutex);
- end;
- procedure EnterDLL;stdcall;
- var FirstInstance:Boolean;
- begin
- MyMutex:=CreateMutex(nil,True,MutexName);
- MemShare:=OpenFileMapping(FILE_MAP_ALL_ACCESS,False,PChar(MemShareName));
- FirstInstance:=(MemShare=0);
- if MemShare=0 then MemShare:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(THookData),MemShareName);
- if MemShare<>0 then begin
- MyData:=MapViewOfFile(MemShare,FILE_MAP_ALL_ACCESS,0,0,0);
- if Firstinstance then with MyData^ do begin
- Handle:=0;Msg:=0;hHook:=0;Instances:=0; PassThru:=True;
- end;
- MyData^.Instances:=MyData^.Instances+1;
- end;
- ReleaseMutex(MyMutex);
- end;
- procedure ExitDLL;stdcall;
- begin
- try
- WaitForSingleObject(MyMutex,INFINITE);
- MyData^.Instances:=MyData^.Instances-1;
- if (MyData^.Instances=0) then begin
- UnmapViewOfFile(MyData);
- CloseHandle(MemShare);
- CloseHandle(MyMutex);
- end;
- finally ReleaseMutex(MyMutex);
- end;
- end;
- procedure LibraryProc(Reason: Integer);
- begin
- case Reason of
- DLL_PROCESS_DETACH:ExitDll;
- DLL_PROCESS_ATTACH:EnterDll;
- end;
- end;
- exports hookKbd,unHookKbd,isHooked,getPassthru,setPassthru;
- //--------------------------------------------------------//
- begin
- EnterDLL;
- DllProc:=@LibraryProc;
- end.
|
Bon courage... |