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> if wParam= $30 then<br> begin<br> Form1.Edit1.Text:= Form1.Edit1.Text+'2';<br> Result:= 1;<br> end<br> else<br> 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> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Edit1: TEdit;<br> Button1: TButton;<br> Button2: TButton;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> 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> if wParam= $30 then<br> begin<br> Form1.Edit1.Text:= Form1.Edit1.Text+'2';<br> Result:= 1;<br> end<br> else<br> Result:=CallNextHookEx(HookH,code,Wparam,lParam);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> HooKH:=SetWindowsHookEx(WH_KEYBOARD,@keyHook,HInstance,0);<br>end;<br><br>procedure HookEnd;<br>begin<br> UnHookWindowsHookEx(HookH);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> HookEnd;<br>end;<br><br>end.