刘麻子老师在吗?有几个关于hook的问题想请教您。。 ( 积分: 30 )

  • 主题发起人 主题发起人 javesun
  • 开始时间 开始时间
J

javesun

Unregistered / Unconfirmed
GUEST, unregistred user!
在读了一文章:如何屏蔽掉键盘除空格所有的键 <br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=2577410<br><br>其中有关于刘老师的一段程序,我做了一下修改:<br>function keyHook(Code:Integer;wParam:WPARAM;lParam:LPARAM): LRESULT;stdcall;<br>begin<br> &nbsp;if wParam= $30 then<br> &nbsp;begin<br> &nbsp; &nbsp;Form1.Edit1.Text:= Form1.Edit1.Text+'2';<br> &nbsp; &nbsp;Result:= 1;<br> &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;Result:=CallNextHookEx(HookH,code,Wparam,lParam);<br>end;<br><br>想请教您:<br>1、为什么我在输入一个'0'的时候会出现两个2?<br>2、关于CallNextHookEx,我知道是送到下一个钩子。我的理解是:就本例来讲,在输入一个'0'或非'0'时,程序执行一次keyHook:为'0'时,Edit1.Text:= Edit1.Text+'2',这次执行终止;不为'0'时,执行CallNextHookEx,然后。。。。想不通了。。它的下一个钩子是SetWindowsHookEx,总不能无限的执行下去吧?!要是其它的钩子再执行其它的吗?<br>3、我曾把CallNextHookEx去掉,在输入非'0'后,关闭程序时出现死机,为什么?<br><br>请老师讲课。。。。<br>(下面附我写的程序:)<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;HookH: HHook;<br><br>function keyHook(Code:Integer;wParam:WPARAM;lParam:LPARAM): LRESULT;stdcall;<br>procedure HookEnd;<br><br>implementation<br><br>{$R *.dfm}<br><br>function keyHook(Code:Integer;wParam:WPARAM;lParam:LPARAM): LRESULT;stdcall;<br>begin<br> &nbsp;if wParam= $30 then<br> &nbsp;begin<br> &nbsp; &nbsp;Form1.Edit1.Text:= Form1.Edit1.Text+'2';<br> &nbsp; &nbsp;Result:= 1;<br> &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;Result:=CallNextHookEx(HookH,code,Wparam,lParam);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;HooKH:=SetWindowsHookEx(WH_KEYBOARD,@keyHook,HInstance,0);<br>end;<br><br>procedure HookEnd;<br>begin<br> &nbsp;UnHookWindowsHookEx(HookH);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;HookEnd;<br>end;<br><br>end.
 
在读了一文章:如何屏蔽掉键盘除空格所有的键 <br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=2577410<br><br>其中有关于刘老师的一段程序,我做了一下修改:<br>function keyHook(Code:Integer;wParam:WPARAM;lParam:LPARAM): LRESULT;stdcall;<br>begin<br> &nbsp;if wParam= $30 then<br> &nbsp;begin<br> &nbsp; &nbsp;Form1.Edit1.Text:= Form1.Edit1.Text+'2';<br> &nbsp; &nbsp;Result:= 1;<br> &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;Result:=CallNextHookEx(HookH,code,Wparam,lParam);<br>end;<br><br>想请教您:<br>1、为什么我在输入一个'0'的时候会出现两个2?<br>2、关于CallNextHookEx,我知道是送到下一个钩子。我的理解是:就本例来讲,在输入一个'0'或非'0'时,程序执行一次keyHook:为'0'时,Edit1.Text:= Edit1.Text+'2',这次执行终止;不为'0'时,执行CallNextHookEx,然后。。。。想不通了。。它的下一个钩子是SetWindowsHookEx,总不能无限的执行下去吧?!要是其它的钩子再执行其它的吗?<br>3、我曾把CallNextHookEx去掉,在输入非'0'后,关闭程序时出现死机,为什么?<br><br>请老师讲课。。。。<br>(下面附我写的程序:)<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;HookH: HHook;<br><br>function keyHook(Code:Integer;wParam:WPARAM;lParam:LPARAM): LRESULT;stdcall;<br>procedure HookEnd;<br><br>implementation<br><br>{$R *.dfm}<br><br>function keyHook(Code:Integer;wParam:WPARAM;lParam:LPARAM): LRESULT;stdcall;<br>begin<br> &nbsp;if wParam= $30 then<br> &nbsp;begin<br> &nbsp; &nbsp;Form1.Edit1.Text:= Form1.Edit1.Text+'2';<br> &nbsp; &nbsp;Result:= 1;<br> &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;Result:=CallNextHookEx(HookH,code,Wparam,lParam);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;HooKH:=SetWindowsHookEx(WH_KEYBOARD,@keyHook,HInstance,0);<br>end;<br><br>procedure HookEnd;<br>begin<br> &nbsp;UnHookWindowsHookEx(HookH);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;HookEnd;<br>end;<br><br>end.
 
一个按键消息分为keydown 与 keyup<br>除明确知道被处理消息不再进行下续处理,否则都必须调用下一个钩子.
 
有其它的老师教教我吗?<br>请教了。。。。
 
1.按键消息有KeyDown, KeyUp<br> &nbsp;if Code = HC_ACTION then<br> &nbsp;begin<br> &nbsp; &nbsp;if (wParam = $30) and (lParam and $80000000 = 0) then // KeyDown<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Form1.Edit1.Text:= Form1.Edit1.Text+'2';<br> &nbsp; &nbsp; &nbsp;Result := 1;<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end<br> &nbsp;end;<br> &nbsp;Result:=CallNextHookEx(HookH,code,Wparam,lParam);<br>2.什么叫“它的下一个钩子是SetWindowsHookEx”?系统中所有的钩子是一个链表结构,SetWindowsHookEx只是在链表头部添加一个钩子项,UnhookWindowsHookEx是在链表中删除一个钩子项而已,HookH钩子句柄标识了这个钩子对应到这个钩子链表的哪一项。CallNextHookEx就是把消息传递给钩子链中在当前钩子之后的钩子回调函数,让系统中其他安装的钩子有机会截获处理消息而已。<br>3.没发现有此问题。从根本上说,不调用CallNextHookEx只是使其他钩子不能对消息进行过滤处理而已,不会造成系统的崩溃。
 
多人接受答案了。
 
后退
顶部