如何得到它的值?(50分)

  • 主题发起人 主题发起人 根本不懂
  • 开始时间 开始时间

根本不懂

Unregistered / Unconfirmed
GUEST, unregistred user!
安装一个键盘、鼠标钩子,如下:<br>Function MyHook(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;<br>请告诉我下面的代码怎样写才能从lparam中得到键盘按下的字符?<br>
 
不止是失望,简直是绝望。这么久都没人回答,随便说两句也好啊!
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,<br>&nbsp; StdCtrls, ExtCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; function Keyhookresult(lP: integer; wP: integer): pchar;<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; hookkey: string;<br>&nbsp; hooktimes: word;<br>&nbsp; hHook: integer;<br>implementation<br>{$R *.DFM}<br><br>function TForm1.Keyhookresult(lP: integer; wP: integer): pchar;<br>begin<br>&nbsp; result := '[Print Screen]';<br>&nbsp; case lp of<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>function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;<br>begin<br>&nbsp; if (peventmsg(lparam)^.message = WM_KEYDOWN) then<br>&nbsp; &nbsp; hookkey := hookkey + Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);<br>&nbsp; if length(hookkey) &gt; 30 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Form1.ListBox1.Items.Add(hookkey);<br>&nbsp; &nbsp; &nbsp; hookkey := TimeToStr(now) + ' &nbsp;';<br>&nbsp; &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>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; 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.<br><br>​
 
说两句[:D][:D][:D]
 
to zw84611:<br>&nbsp; &nbsp; 不好意思,我胡乱测试了一下,好像你的程序和 F12 键有仇一样,多按几下就死了,<br>幸好我是win2000,还回得来。<br><br>&nbsp; &nbsp; 另外,不能返回大小写字符。
 
干嘛要从lParam得到?wParam不是有VK代码吗?<br>MapVirtualKey可以翻译scancode
 
刚才 F12 的现象只有在调试运行时才有,单独执行 EXE 时没有这个问题,奇怪?<br>不过, ALT 检测不出, ALT 旁的 windows 键检测出来都是 Print Screen 。
 
这是《不用DLL文件的键盘HOOK》里面的。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
816
SUNSTONE的Delphi笔记
S
S
回复
0
查看
731
SUNSTONE的Delphi笔记
S
后退
顶部