delphi键盘钩子没效果

delphi键盘钩子没效果,第1张

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

Button2: TButton;

Button3: TButton;

procedure FormCreate(Sender: TObject);

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

EventArr:array[01000]of EVENTMSG;

EventLog:Integer;

PlayLog:Integer;

hHook,hPlay:Integer;

bDelay:Bool;

implementation

{$R DFM}

Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;

begin

Result:=0;

if iCode < 0 then

Result := CallNextHookEx(hPlay,iCode,wParam,lParam)

else if iCode = HC_SYSMODALON then

// canPlay:=0

else if iCode = HC_SYSMODALOFF then

// canPlay:=1

else if (iCode=HC_GETNEXT) then begin

if bDelay then begin

bDelay:=False;

Result:=50;

end;

pEventMSG(lParam)^:=EventArr[PlayLog];

end

else if (iCode = HC_SKIP)then begin

bDelay := True;

PlayLog:=PlayLog+1;

end;

if PlayLog>=EventLog then begin

UNHookWindowsHookEx(hPlay);

end;

end;

function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;

begin

// recOK:=1;

Result:=0;

if iCode < 0 then

Result := CallNextHookEx(hHook,iCode,wParam,lParam)

else if iCode = HC_SYSMODALON then

// recOK:=0

else if iCode = HC_SYSMODALOFF then

// recOK:=1

else if (iCode = HC_ACTION) then begin

EventArr[EventLog]:=pEventMSG(lParam)^;

EventLog:=EventLog+1;

if EventLog>=1000 then begin

UnHookWindowsHookEx(hHook);

end;

end;

end;

procedure TForm1FormCreate(Sender: TObject);

begin

Button2Enabled:=False;

Button3Enabled:=False;

end;

procedure TForm1Button1Click(Sender: TObject);

begin

EventLog:=0;

hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);

Button2Enabled:=True;

Button1Enabled:=False;

Button3Enabled:=False;

end;

procedure TForm1Button3Click(Sender: TObject);

begin

PlayLog:=0;

hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,

HInstance,0);

end;

procedure TForm1Button2Click(Sender: TObject);

begin

UnHookWindowsHookEx(hHook);

hHook:=0;

Button1Enabled:=True;

Button2Enabled:=False;

Button3Enabled:=True;

end;

end

以上是delphi核心编程里 钩子原理:鼠标键盘记录与回放中的代码,一个form,三个按钮

API钩子很简单,源码我没有,如果你会用API的话,我给你讲一下实现的过程吧:先用GetModuleHandle函数获取dll的模块句柄,再用GetProcAddress函数获取要HOOK的函数地址

下一步用VirtualProtect函数将要HOOK的函数所在地址的属性设置为可写,最后一步,将新函数地址(用于替换原函数)写到原函数入口地址替换掉原函数地址,这样就实现了API钩子功能了

就是用这个方法获取指定线程ID:GetWindowThreadProcessId

如果实在不行的话可以搞个全局的钩子(参数为0),这样也行的。

当用的是全局的钩子时:

wParam 表示当前所捕捉按键状态(按下或者抬起)

lParam 表示当前所捕捉按键的值

用GetClassName可以获取窗体类名

[DllImport("user32dll")]

public static extern int GetClassName(

int hwnd,

StringBuilder lpClassName,

int nMaxCount

);

以上就是关于delphi键盘钩子没效果全部的内容,包括:delphi键盘钩子没效果、delphi怎么HOOK指定API函数!、关于钩子(Hook)的问题 [Delphi]等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10219560.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存