windows Api 一问(30分)

  • 主题发起人 主题发起人 朱浩刚
  • 开始时间 开始时间

朱浩刚

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么我做的键盘hook程序每次截获的键盘按键都是成双的?还望那位高手指点。<br>(如键入a,记录按键的文件中却是aa.当我把记录的程序段去掉,着什么都没有)<br><br>处理键盘消息的函数:<br>function keyboardhook(icode:integer;wparam:wparam; lparam:lparam):lresult;stdcall;export;<br>begin<br>&nbsp;if icode&lt;0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; result:=callnexthookex(nexthookproc,icode,wparam,lparam);<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp;end;<br>assignfile(debugfile,afilename);<br>append(debugfile);<br>if getkeystate(vk_return)&lt;0 then<br>&nbsp;begin<br>&nbsp; &nbsp;writeln(debugfile,'');<br>&nbsp;end<br>else<br>&nbsp;write(debugfile,char(wparam));<br>&nbsp;closefile(debugfile);<br>&nbsp;result:=0;<br>end;
 
icode&gt;=0时也应该CallNextHookEx
 
我觉得不是这一段出问题,,可能是程序其它部分出问题了..
 
截获键盘消息有没有时间设定?<br>&nbsp;如果我按键按的快一些,如z h 则显示(zhzh),如果慢一点则显示(zzhh).这到底是<br>&nbsp;则么回事呀?望高手出招。
 
if icode&lt;=0
 
如果用户按下的是字符键,那么WINDOWS向你发送WM_KEYDOWN和WM_CHAR两个消息,所以你<br>会记录下两次按键!
 
为什么你要在hook里assignfile、appendfile、writefile最后closefile呢?这么做不<br>太妥吧?
 
如何处理呢?
 
我不是说过了吗,如果用户按下的是字符键,那么WINDOWS向你发送WM_KEYDOWN和WM_CHAR两<br>个消息,而在你的程序里用write(debugfile,char(wparam));也就是说你的程序只记录字符<br>键,那当然会记录下两次按键了!如果你试着记录其它键如Ctrl等,你会发现只记录下一个!<br>&gt;如果我按键按的快一些,如z h 则显示(zhzh),如果慢一点则显示(zzhh)<br>这应该算是系统的延迟!
 
如何解决呢?<br>我也遇到一样的问题!
 
只拦截WM_KEYDOWN消息不就行了.
 
那着样只拦截wm_keydown呢,好象setwindowshookex中没有选项可设置成专门响应一种消息的。<br>
 
另外我的钩子程序,好象不能endhook. 不知何故,请大家帮我看看。<br><br><br>var nexthookproc:hhook; &nbsp; &nbsp;//handle to a hook<br>&nbsp; &nbsp; procsaveexit:pointer;<br><br>function keyboardhook(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export;<br><br>function setkeyhook:bool;export;//加载钩子<br><br>function endkeyhook:bool;export;//卸载钩子<br><br>procedure keyhookexit;far;<br><br>const afilename='c:/debug.txt';//将键盘输入动作写入文件中<br><br>var debugfile:textfile;<br>&nbsp; &nbsp; &nbsp;hookhandle:hhook;<br><br>implementation<br><br>function keyboardhook(icode:integer;wparam:wparam; lparam:lparam):lresult;stdcall;export;<br>begin<br>&nbsp;if (icode&lt;0) then &nbsp; <br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; result:=callnexthookex(nexthookproc,icode,wparam,lparam);<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp;end;<br><br>assignfile(debugfile,afilename);<br>append(debugfile);<br>if getkeystate(vk_return)&lt;0 then<br>&nbsp;begin<br>&nbsp; &nbsp;writeln(debugfile,'');<br>&nbsp;end<br>else<br>&nbsp;write(debugfile,char(wparam));<br>&nbsp;closefile(debugfile);<br>&nbsp;result:=0;<br>end;<br><br><br>function endkeyhook:bool;export;<br>begin<br>&nbsp;if nexthookproc&lt;&gt;0 then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; unhookwindowshookex(nexthookproc);<br>&nbsp; &nbsp; nexthookproc:=0;<br>&nbsp; &nbsp; messagebeep(0);<br>&nbsp; &nbsp;end;<br>result:=nexthookproc=0;<br>end;<br><br><br><br>function setkeyhook:bool;export;<br>begin<br>&nbsp;setwindowshookex(wh_keyboard,keyboardhook,hinstance,0);<br>&nbsp;result:=true;<br>end;<br><br><br><br><br>procedure keyhookexit;far;<br>begin<br>&nbsp;if nexthookproc&lt;&gt;0 then &nbsp;//如果还有hook程序在hook队列中.<br>&nbsp; endkeyhook;<br>&nbsp; exitproc:=procsaveexit;<br>end;<br>end.<br><br>
 
接受答案了.
 

Similar threads

后退
顶部