鼠标钩子 可以不用 dll文件 直接写在单元里面 吗?(100)

  • 主题发起人 主题发起人 hepushu
  • 开始时间 开始时间
H

hepushu

Unregistered / Unconfirmed
GUEST, unregistred user!
我想获取系统范围内的鼠标点击消息,只能用钩子吗?
 
不可以。所有的hook,必须写成DLL。
 
楼上的,不懂可别乱说我贴两个无DLL的例子1、鼠标键盘记录回放unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; EventArr:array[0..1000]of EVENTMSG; EventLog:Integer; PlayLog:Integer; hHook,hPlay:Integer; bDelay:Bool;implementation{$R *.DFM}Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;begin Result:=0; if iCode < 0 then Result := CallNextHookEx(hPlay,iCode,wParam,lParam) else if iCode = HC_SYSMODALON then // canPlay:=0 else if iCode = HC_SYSMODALOFF then // canPlay:=1 else if (iCode=HC_GETNEXT) then begin if bDelay then begin bDelay:=False; Result:=50; end; pEventMSG(lParam)^:=EventArr[PlayLog]; end else if (iCode = HC_SKIP)then begin bDelay := True; PlayLog:=PlayLog+1; end; if PlayLog>=EventLog then begin UNHookWindowsHookEx(hPlay); end;end;function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;begin// recOK:=1; Result:=0; if iCode < 0 then Result := CallNextHookEx(hHook,iCode,wParam,lParam) else if iCode = HC_SYSMODALON then// recOK:=0 else if iCode = HC_SYSMODALOFF then// recOK:=1 else if (iCode = HC_ACTION) then begin EventArr[EventLog]:=pEventMSG(lParam)^; EventLog:=EventLog+1; if EventLog>=1000 then begin UnHookWindowsHookEx(hHook); end; end;end;procedure TForm1.FormCreate(Sender: TObject);begin Button2.Enabled:=False; Button3.Enabled:=False;end;procedure TForm1.Button1Click(Sender: TObject);begin EventLog:=0; hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0); Button2.Enabled:=True; Button1.Enabled:=False; Button3.Enabled:=False;end;procedure TForm1.Button3Click(Sender: TObject);begin PlayLog:=0; hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc, HInstance,0);end;procedure TForm1.Button2Click(Sender: TObject);begin UnHookWindowsHookEx(hHook); hHook:=0; Button1.Enabled:=True; Button2.Enabled:=False; Button3.Enabled:=True;end;end.==============================2、不用DLL的KeyHookunit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, StdCtrls, ExtCtrls;type TForm1 = class(TForm) ListBox1: TListBox; Button1: TButton; Button2: TButton; Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Label2: TLabel; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure ListBox1DblClick(Sender: TObject); procedure Edit1Change(Sender: TObject); procedure Edit1KeyPress(Sender: TObject; var Key: Char); private function Keyhookresult(lP: integer; wP: integer): pchar; { Private declarations } public { Public declarations } end;var Form1: TForm1; hookkey: string; hooktimes: word; hHook: integer;implementation{$R *.DFM}function TForm1.Keyhookresult(lP: integer; wP: integer): pchar;begin result := '[Print Screen]';{ VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) }{ VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) } case lp of 14354: result := '[Alt]'; //不能识别 10688: result := '`'; 561: Result := '1'; 818: result := '2'; 1075: result := '3'; 1332: result := '4'; 1589: result := '5'; 1846: result := '6'; 2103: result := '7'; 2360: result := '8'; 2617: result := '9'; 2864: result := '0'; 3261: result := '-'; 3515: result := '='; 4177: result := 'Q'; 4439: result := 'W'; 4677: result := 'E'; 4946: result := 'R'; 5204: result := 'T'; 5465: result := 'Y'; 5717: result := 'U'; 5961: result := 'I'; 6223: result := 'O'; 6480: result := 'P'; 6875: result := '['; 7133: result := ']'; 11228: result := '/'; 7745: result := 'A'; 8019: result := 'S'; 8260: result := 'D'; 8518: result := 'F'; 8775: result := 'G'; 9032: result := 'H'; 9290: result := 'J'; 9547: result := 'K'; 9804: result := 'L'; 10170: result := ';'; 10462: result := ''''; 11354: result := 'Z'; 11608: result := 'X'; 11843: result := 'C'; 12118: result := 'V'; 12354: result := 'B'; 12622: result := 'N'; 12877: result := 'M'; 13244: result := ','; 13502: result := '.'; 13759: result := '/'; 13840: result := '[Right-Shift]'; 14624: result := '[Space]'; 283: result := '[Esc]'; 15216: result := '[F1]'; 15473: result := '[F2]'; 15730: result := '[F3]'; 15987: result := '[F4]'; 16244: result := '[F5]'; 16501: result := '[F6]'; 16758: result := '[F7]'; 17015: result := '[F8]'; 17272: result := '[F9]'; 17529: result := '[F10]'; 22394: result := '[F11]'; 22651: result := '[F12]'; 10768: Result := '[Left-Shift]'; 14868: result := '[CapsLock]'; 3592: result := '[Backspace]'; 3849: result := '[Tab]'; 7441: if wp > 30000 then result := '[Right-Ctrl]' else result := '[Left-Ctrl]'; 13679: result := '[Num /]'; 17808: result := '[NumLock]'; 300: result := '[Print Screen]'; 18065: result := '[Scroll Lock]'; 17683: result := '[Pause]'; 21088: result := '[Num0]'; 21358: result := '[Num.]'; 20321: result := '[Num1]'; 20578: result := '[Num2]'; 20835: result := '[Num3]'; 19300: result := '[Num4]'; 19557: result := '[Num5]'; 19814: result := '[Num6]'; 18279: result := '[Num7]'; 18536: result := '[Num8]'; 18793: result := '[Num9]'; 19468: result := '[*5*]'; 14186: result := '[Num *]'; 19053: result := '[Num -]'; 20075: result := '[Num +]'; 21037: result := '[Insert]'; 21294: result := '[Delete]'; 18212: result := '[Home]'; 20259: result := '[End]'; 18721: result := '[PageUp]'; 20770: result := '[PageDown]'; 18470: result := '[UP]'; 20520: result := '[DOWN]'; 19237: result := '
'; 19751: result := '
'; 7181: result := '[Enter]'; end;end;//钩子回调过程function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;var s:string;begin if (PEventMsg(lparam)^.message = WM_KEYDOWN) then begin //事件消息,键盘按下 s:=format('Down:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH); Form1.ListBox1.Items.Add(s); end else if (PEventMsg(lparam)^.message = WM_KEYUP) then begin //键盘按键 s:=format(' Up:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH); Form1.ListBox1.Items.Add(s); end;end;procedure TForm1.FormCreate(Sender: TObject);begin hooktimes := 0; hHook := 0;end;procedure TForm1.Button1Click(Sender: TObject);begin inc(hooktimes); if hooktimes = 1 then begin hookkey := TimeToStr(now) + ' '; hHook := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0); MessageBox(0, '键盘监视启动', '信息', MB_ICONINFORMATION + MB_OK); end;end;procedure TForm1.Button2Click(Sender: TObject);begin UnHookWindowsHookEx(hHook); hHook := 0; if hooktimes <> 0 then begin MessageBox(0, '键盘监视关闭', '信息', MB_ICONINFORMATION + MB_OK); end; hooktimes := 0;end;procedure TForm1.ListBox1DblClick(Sender: TObject);begin listbox1.clear;end;procedure TForm1.Edit1Change(Sender: TObject);var i:DWORD;begin if length(edit1.text)<>1 then exit; //映射虚拟键 i:=MapVirtualKey(ord(edit1.text[1]), 0 ); edit2.text:=format('%d %x',[i,i]);end;procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);begin edit1.text:='';end;end.​
 
呵呵,学习!
 
本进程的 可以
 
hfghfghfg, 正解
 
正解个屁,不懂别乱说. LL 的就可以了。
 
系统钩子必须放在dll中。其他的系统钩子只有键盘记录钩子才可以不用dll的,因为操作系统是通过挂靠虚拟输入队列来实现键盘钩子的。默认情况下只有当前获得输入焦点的程序才有虚拟输入队列。设置了键盘钩子后,程序没有输入焦点时操作系统也给予连接虚拟输入队列。
 
鼠标也可以的
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部