键盘钩子代码问题还是其它问题???(50分)

  • 主题发起人 主题发起人 xiaoxingchi
  • 开始时间 开始时间
X

xiaoxingchi

Unregistered / Unconfirmed
GUEST, unregistred user!
问题:<br> &nbsp; &nbsp;代码可以用,也能捕捉到 按键 。可是为什么当 开始 捕捉键盘输入的时候,鼠标会消失呢?怎么解决?问题出在哪里?<br> &nbsp; &nbsp;谢谢大家了,菜鸟初学Delphi.<br> &nbsp; &nbsp;这个代码是CSDN的网友给的。 <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;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;ListBox1: TListBox;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;Edit2: TEdit;<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure ListBox1DblClick(Sender: TObject);<br> &nbsp; &nbsp;procedure Edit1Change(Sender: TObject);<br> &nbsp; &nbsp;procedure Edit1KeyPress(Sender: TObject; var Key: Char);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp; &nbsp;function Keyhookresult(lP: integer; wP: integer): pchar;<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;hookkey: string;<br> &nbsp;hooktimes: word;<br> &nbsp;hHook: integer;<br><br>implementation<br><br>{$R *.dfm}<br><br>function TForm1.Keyhookresult(lP: integer; wP: integer): pchar;<br>begin<br> &nbsp;result := '[Print Screen]';<br>{ VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) }<br>{ VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) }<br> &nbsp;case lp of<br> &nbsp; &nbsp;14354: result := '[Alt]'; //不能识别<br> &nbsp; &nbsp;10688: result := '`';<br> &nbsp; &nbsp;561: Result := '1';<br> &nbsp; &nbsp;818: result := '2';<br> &nbsp; &nbsp;1075: result := '3';<br> &nbsp; &nbsp;1332: result := '4';<br> &nbsp; &nbsp;1589: result := '5';<br> &nbsp; &nbsp;1846: result := '6';<br> &nbsp; &nbsp;2103: result := '7';<br> &nbsp; &nbsp;2360: result := '8';<br> &nbsp; &nbsp;2617: result := '9';<br> &nbsp; &nbsp;2864: result := '0';<br> &nbsp; &nbsp;3261: result := '-';<br> &nbsp; &nbsp;3515: result := '=';<br> &nbsp; &nbsp;4177: result := 'Q';<br> &nbsp; &nbsp;4439: result := 'W';<br> &nbsp; &nbsp;4677: result := 'E';<br> &nbsp; &nbsp;4946: result := 'R';<br> &nbsp; &nbsp;5204: result := 'T';<br> &nbsp; &nbsp;5465: result := 'Y';<br> &nbsp; &nbsp;5717: result := 'U';<br> &nbsp; &nbsp;5961: result := 'I';<br> &nbsp; &nbsp;6223: result := 'O';<br> &nbsp; &nbsp;6480: result := 'P';<br> &nbsp; &nbsp;6875: result := '[';<br> &nbsp; &nbsp;7133: result := ']';<br> &nbsp; &nbsp;11228: result := '/';<br> &nbsp; &nbsp;7745: result := 'A';<br> &nbsp; &nbsp;8019: result := 'S';<br> &nbsp; &nbsp;8260: result := 'D';<br> &nbsp; &nbsp;8518: result := 'F';<br> &nbsp; &nbsp;8775: result := 'G';<br> &nbsp; &nbsp;9032: result := 'H';<br> &nbsp; &nbsp;9290: result := 'J';<br> &nbsp; &nbsp;9547: result := 'K';<br> &nbsp; &nbsp;9804: result := 'L';<br> &nbsp; &nbsp;10170: result := ';';<br> &nbsp; &nbsp;10462: result := '''';<br> &nbsp; &nbsp;11354: result := 'Z';<br> &nbsp; &nbsp;11608: result := 'X';<br> &nbsp; &nbsp;11843: result := 'C';<br> &nbsp; &nbsp;12118: result := 'V';<br> &nbsp; &nbsp;12354: result := 'B';<br> &nbsp; &nbsp;12622: result := 'N';<br> &nbsp; &nbsp;12877: result := 'M';<br> &nbsp; &nbsp;13244: result := ',';<br> &nbsp; &nbsp;13502: result := '.';<br> &nbsp; &nbsp;13759: result := '/';<br> &nbsp; &nbsp;13840: result := '[Right-Shift]';<br> &nbsp; &nbsp;14624: result := '[Space]';<br> &nbsp; &nbsp;283: result := '[Esc]';<br> &nbsp; &nbsp;15216: result := '[F1]';<br> &nbsp; &nbsp;15473: result := '[F2]';<br> &nbsp; &nbsp;15730: result := '[F3]';<br> &nbsp; &nbsp;15987: result := '[F4]';<br> &nbsp; &nbsp;16244: result := '[F5]';<br> &nbsp; &nbsp;16501: result := '[F6]';<br> &nbsp; &nbsp;16758: result := '[F7]';<br> &nbsp; &nbsp;17015: result := '[F8]';<br> &nbsp; &nbsp;17272: result := '[F9]';<br> &nbsp; &nbsp;17529: result := '[F10]';<br> &nbsp; &nbsp;22394: result := '[F11]';<br> &nbsp; &nbsp;22651: result := '[F12]';<br> &nbsp; &nbsp;10768: Result := '[Left-Shift]';<br> &nbsp; &nbsp;14868: result := '[CapsLock]';<br> &nbsp; &nbsp;3592: result := '[Backspace]';<br> &nbsp; &nbsp;3849: result := '[Tab]';<br> &nbsp; &nbsp;7441:<br> &nbsp; &nbsp; &nbsp;if wp &gt; 30000 then<br> &nbsp; &nbsp; &nbsp; &nbsp;result := '[Right-Ctrl]'<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;result := '[Left-Ctrl]';<br> &nbsp; &nbsp;13679: result := '[Num /]';<br> &nbsp; &nbsp;17808: result := '[NumLock]';<br> &nbsp; &nbsp;300: result := '[Print Screen]';<br> &nbsp; &nbsp;18065: result := '[Scroll Lock]';<br> &nbsp; &nbsp;17683: result := '[Pause]';<br> &nbsp; &nbsp;21088: result := '[Num0]';<br> &nbsp; &nbsp;21358: result := '[Num.]';<br> &nbsp; &nbsp;20321: result := '[Num1]';<br> &nbsp; &nbsp;20578: result := '[Num2]';<br> &nbsp; &nbsp;20835: result := '[Num3]';<br> &nbsp; &nbsp;19300: result := '[Num4]';<br> &nbsp; &nbsp;19557: result := '[Num5]';<br> &nbsp; &nbsp;19814: result := '[Num6]';<br> &nbsp; &nbsp;18279: result := '[Num7]';<br> &nbsp; &nbsp;18536: result := '[Num8]';<br> &nbsp; &nbsp;18793: result := '[Num9]';<br> &nbsp; &nbsp;19468: result := '[*5*]';<br> &nbsp; &nbsp;14186: result := '[Num *]';<br> &nbsp; &nbsp;19053: result := '[Num -]';<br> &nbsp; &nbsp;20075: result := '[Num +]';<br> &nbsp; &nbsp;21037: result := '[Insert]';<br> &nbsp; &nbsp;21294: result := '[Delete]';<br> &nbsp; &nbsp;18212: result := '[Home]';<br> &nbsp; &nbsp;20259: result := '[End]';<br> &nbsp; &nbsp;18721: result := '[PageUp]';<br> &nbsp; &nbsp;20770: result := '[PageDown]';<br> &nbsp; &nbsp;18470: result := '[UP]';<br> &nbsp; &nbsp;20520: result := '[DOWN]';<br> &nbsp; &nbsp;19237: result := '
';<br> &nbsp; &nbsp;19751: result := '
';<br> &nbsp; &nbsp;7181: result := '[Enter]';<br> &nbsp;end;<br>end;<br><br>//钩子回调过程<br>function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;<br>var<br> &nbsp; s:string;<br>begin<br> &nbsp;{if (PEventMsg(lparam)^.message = WM_KEYDOWN) then<br> &nbsp;begin<br> &nbsp; &nbsp; //事件消息,键盘按下<br> &nbsp; &nbsp; s:=format('Down:%5d %5d &nbsp;',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);<br> &nbsp; &nbsp; Form1.ListBox1.Items.Add(s);<br> &nbsp;end<br> &nbsp;else if (PEventMsg(lparam)^.message = WM_KEYUP) then<br> &nbsp;begin<br> &nbsp; &nbsp; //键盘按键<br> &nbsp; &nbsp; s:=format(' &nbsp;Up:%5d %5d &nbsp;',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH);<br> &nbsp; &nbsp; Form1.ListBox1.Items.Add(s);<br> &nbsp;end;}<br> &nbsp;if (PEventMsg(lparam)^.message = WM_KEYDOWN) then<br> &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;//s:=format('Down:%5d %5d &nbsp;',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);<br> &nbsp; &nbsp; &nbsp; &nbsp;s:=Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);<br> &nbsp; &nbsp; &nbsp; &nbsp;form1.ListBox1.Items.Add(s);<br> &nbsp;end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> &nbsp;hooktimes := 0;<br> &nbsp;hHook := 0;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;inc(hooktimes);<br> &nbsp;if hooktimes = 1 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;hookkey := TimeToStr(now) + ' &nbsp;';<br> &nbsp; &nbsp; &nbsp;hHook := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);<br> &nbsp; &nbsp; &nbsp;//MessageBox(0, '键盘监视启动', '信息', MB_ICONINFORMATION + MB_OK);<br> &nbsp; &nbsp;end;<br>end;<br><br><br>procedure TForm1.ListBox1DblClick(Sender: TObject);<br>begin<br> &nbsp; listbox1.clear;<br>end;<br><br>procedure TForm1.Edit1Change(Sender: TObject);<br>var<br> &nbsp; i:DWORD;<br>begin<br> &nbsp; if length(edit1.text)&lt;&gt;1 then exit;<br> &nbsp; //映射虚拟键<br> &nbsp; i:=MapVirtualKey(ord(edit1.text[1]), 0 );<br> &nbsp; edit2.text:=format('%d %x',[i,i]);<br>end;<br><br>procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);<br>begin<br> &nbsp; edit1.text:='';<br>end;<br><br><br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>UnHookWindowsHookEx(hHook);<br> &nbsp;hHook := 0;<br> &nbsp; {if hooktimes &lt;&gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;MessageBox(0, '键盘监视关闭', '信息', MB_ICONINFORMATION + MB_OK);<br> &nbsp; &nbsp;end;}<br> &nbsp;hooktimes := 0;<br>end;<br><br>end.​
 

Similar threads

后退
顶部