type
TForm1 = class(TForm)
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall;
function HookOn(lpHwnd:HWND;lpType:Longint):Longint;stdcall;export;
function HookOff:Boolean;stdcall;export;
implementation
var
hHk: HHOOK=0; //钩子的句柄
hThread: Cardinal; //长整形数据,窗口线程的标示符
hmod: Pointer; //进程标示符的指针
{$R *.dfm}
function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall;
begin
if (wParam=VK_END) and ((LParam and $40000000) <> 0) then
begin
Form1:=TForm1.Create(Application);
Form1.show;
end;
Result :=0 //CallNextHookEx(hHk,nCode,WParam,LParam);
end;
function HookOn(lpHwnd:HWND;lpType:Longint): Longint;stdcall; export;
begin
hThread :=GetWindowThreadProcessId(lpHwnd,hmod);
//注入开始
hHk :=SetWindowsHookEx(lpType,@HookProc,hInstance,hThread); // WH_KEYBOARD
Result :=hHk
end;
function HookOff:Boolean;stdcall; export;
begin
if hHk <> 0 then
begin
//移除挂钩
UnHookWindowsHookEx(hHk);
hHk :=0;
Result :=true;
end
else
Result :=false;
end;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function HookOn(lpHwnd:HWND;lpType:Longint):Longint;stdcall;external 'Hook32.dll' name 'HookOn';
function HookOff:Boolean;stdcall;external 'Hook32.dll' name 'HookOff';
implementation
{$R *.dfm}
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
hookoff;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
h1:HWND;
begin
h1:=FindWindow(NIL,'counter-strike');//这是窗口的句柄,要自己找到后,填写入。
if h1=0 then showmessage('没找到进程!');
if h1> 0 then showmessage('找到进程!');
sleep(200);
HookOn(h1,WH_KEYBOARD);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
HookOff;
end;
function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall;
begin
if (wParam=VK_END) and ((LParam and $40000000) <> 0) then
begin
if Form1 =nil then
begin
Form1:=TForm1.Create(Application);
Form1.show;
end
else......
end;
Result :=0 //CallNextHookEx(hHk,nCode,WParam,LParam);
if nCode < 0 then //必须将消息传递到消息链的下一个接收单元
Result := CallNextHookEx(hHk, // hHook值是SetWindowsHookEx()的传回值
nCode, wParam, lParam); //nCode、wParam、lParam则是Hook函数中的三个参数
end;