如何HOOK系统所有的键盘鼠标事件 需要做一个等待多少空闲时间的程序,有点象屏幕保护的功能 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用 SetWindowsHookEx(...) 函数挂接鼠标和键盘钩子,如:HHOOK hHookMouse = NULL;HHOOK hHookKeyBD = NULL;HMODULE hHookDll = NULL;hHookDll = LoadLibrary("Hook.dll");//Hook.dll 中必须导出鼠标和键盘钩子的回调函数//我们假定鼠标钩子的回调函数为 MouseHookProc// 键盘钩子的回调函数为 KeybdHookProchHookMouse = SetWindowsHookEx(WH_MOUSE,MouseHookProc,hHookDll,0L);hHookKeyBD = SetWindowsHookEx(WH_KEYBOARD,KeybdHookProc,hHookDll,0L);//注意要将 hHookMouse 和 hHookKeyBD 两个句柄传入 Hook.dll 中,因为在回调函数中需要用到这两个句柄:)//还有,我是用 C 语法写的,至于怎么转为 DELPHI 写法,应该很容易吧:) 补充:1、计时可以在回调函数中实现。2、因为是全局钩子,所以回调函数必须放在 DLL 中,否则这两个钩子只在本进程中有效:)3、本人正在学习 DELPHI ,如果大虾把程序转成了 DELPHI 写法,请贴出来!!! 我給你代碼://鼠標unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Edit1: TEdit; 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; recOK:Integer; canPlay:Integer; bDelay:Bool;implementation{$R *.dfm}Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall; begin canPlay:=1; 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 ((canPlay =1 )and(iCode=HC_GETNEXT)) then begin if bDelay then begin bDelay:=False; Result:=50; end; pEventMSG(lParam)^:=EventArr[PlayLog]; end else if ((canPlay = 1)and(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 thenResult := CallNextHookEx(hHook,iCode,wParam,lParam)else if iCode = HC_SYSMODALON thenrecOK:=0else if iCode = HC_SYSMODALOFF thenrecOK:=1else if ((recOK>0) and (iCode = HC_ACTION)) then beginEventArr[EventLog]:=pEventMSG(lParam)^;EventLog:=EventLog+1;if EventLog>=1000 then beginUnHookWindowsHookEx(hHook);end;end;end;procedure TForm1.FormCreate(Sender: TObject);beginButton1.Caption:='記錄';Button2.Caption:='停止';Button3.Caption:='回放';Button4.Caption:='范例';Button2.Enabled:=False;Button3.Enabled:=False;end;procedure TForm1.Button1Click(Sender: TObject);beginEventLog:=0;hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);Button2.Enabled:=True;Button1.Enabled:=False;end;procedure TForm1.Button2Click(Sender: TObject);beginUnHookWindowsHookEx(hHook);hHook:=0;Button1.Enabled:=True;Button2.Enabled:=False;Button3.Enabled:=True;end;procedure TForm1.Button3Click(Sender: TObject);beginPlayLog:=0;hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,HInstance,0);Button3.Enabled:=False;end;end.//鍵盤unit KB_u;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus,ShellApi;Const MI_iconEvent=Wm_user+1;type TKBForm = class(TForm) PopupMenu1: TPopupMenu; SetHook1: TMenuItem; EndHook1: TMenuItem; Close1: TMenuItem; procedure FormCreate(Sender: TObject); procedure SetHook1Click(Sender: TObject); procedure EndHook1Click(Sender: TObject); procedure Close1Click(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormPaint(Sender: TObject); private { Private declarations } nid:NotifyIconData; NormalIcon:Ticon; public { Public declarations } Procedure Icontray(Var Msg:TMessage); Message Mi_IConEvent; end;var KBForm: TKBForm;implementation{$R *.dfm}function SetKeyHook:bool;External 'KeySPY.dll';function EndKeyHook:bool;External 'KeySPY.dll';Procedure TKBForm.Icontray(Var Msg:TMessage);var pt:Tpoint;beginif Msg.LParam=wm_lbuttondown then sethook1Click(Self);if Msg.LParam=Wm_rbuttondown then begin GetCursorPos(pt); SetForegroundWindow(Handle); PopupMenu1.Popup(Pt.x,Pt.Y); end;end;procedure TKBForm.FormCreate(Sender: TObject);beginNormalIcon:=Ticon.Create;Application.Title:=Caption;nid.cbSize:=Sizeof(nil);nid.Wnd:=Handle;nid.uID:=0;nid.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;nid.uCallbackMessage:=MI_iconEvent;nid.hIcon:=Application.Icon.Handle;StrCopy(nid.szTip,pchar(Caption));nid.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;if not Shell_NotifyIcon(nim_add,@nid) then begin ShowMessage('失敗'); Application.Terminate; end;SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);end;procedure TKBForm.SetHook1Click(Sender: TObject);beginSetkeyhook;end;procedure TKBForm.EndHook1Click(Sender: TObject);beginEndkeyhook;end;procedure TKBForm.Close1Click(Sender: TObject);beginApplication.Terminate;end;procedure TKBForm.FormDestroy(Sender: TObject);beginnid.uFlags:=0;Shell_NotifyIcon(nim_delete,@nid);end;procedure TKBForm.FormPaint(Sender: TObject);beginHide;end;end. ReportMachine是免费的吗? dxdbtreeview每次关闭的时候都要遍历所有的根节点的吗? 如何提高查询速度?各位大虾请进!在线等,急急急! 如何取出X年X月有多少天? 关于报表的问题。 谁能贴一个关于TQUERY,TTABLE等控件的数据库应用的详细使用 后天到某软件公司面试,大家帮我,他们会出什么样的面试题? Delphi5的Code Insight的问题?例如我键入Query1.Ope时,弹出的下拉框不是Open 关于CSDN 高手请进,救命 如何解决简体与繁体版本切换,程序字体的问题? 200分送上,数据200万点如何打印?
HHOOK hHookKeyBD = NULL;
HMODULE hHookDll = NULL;hHookDll = LoadLibrary("Hook.dll");
//Hook.dll 中必须导出鼠标和键盘钩子的回调函数
//我们假定鼠标钩子的回调函数为 MouseHookProc
// 键盘钩子的回调函数为 KeybdHookProchHookMouse = SetWindowsHookEx(WH_MOUSE,MouseHookProc,hHookDll,0L);
hHookKeyBD = SetWindowsHookEx(WH_KEYBOARD,KeybdHookProc,hHookDll,0L);//注意要将 hHookMouse 和 hHookKeyBD 两个句柄传入 Hook.dll 中,因为在回调函数中需要用到这两个句柄:)
//还有,我是用 C 语法写的,至于怎么转为 DELPHI 写法,应该很容易吧:)
2、因为是全局钩子,所以回调函数必须放在 DLL 中,否则这两个钩子只在本进程中有效:)
3、本人正在学习 DELPHI ,如果大虾把程序转成了 DELPHI 写法,请贴出来!!!
//鼠標
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Edit1: TEdit;
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; recOK:Integer; canPlay:Integer; bDelay:Bool;implementation{$R *.dfm}Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall; begin canPlay:=1; 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 ((canPlay =1 )and(iCode=HC_GETNEXT)) then begin if bDelay then begin bDelay:=False; Result:=50; end; pEventMSG(lParam)^:=EventArr[PlayLog]; end else if ((canPlay = 1)and(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 ((recOK>0) and (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
Button1.Caption:='記錄';
Button2.Caption:='停止';
Button3.Caption:='回放';
Button4.Caption:='范例';
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;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook);
hHook:=0;
Button1.Enabled:=True;
Button2.Enabled:=False;
Button3.Enabled:=True;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
PlayLog:=0;
hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,
HInstance,0);
Button3.Enabled:=False;
end;end.
//鍵盤
unit KB_u;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus,ShellApi;Const MI_iconEvent=Wm_user+1;type
TKBForm = class(TForm)
PopupMenu1: TPopupMenu;
SetHook1: TMenuItem;
EndHook1: TMenuItem;
Close1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure SetHook1Click(Sender: TObject);
procedure EndHook1Click(Sender: TObject);
procedure Close1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject); private
{ Private declarations }
nid:NotifyIconData;
NormalIcon:Ticon;
public
{ Public declarations }
Procedure Icontray(Var Msg:TMessage);
Message Mi_IConEvent;
end;var
KBForm: TKBForm;implementation{$R *.dfm}function SetKeyHook:bool;External 'KeySPY.dll';
function EndKeyHook:bool;External 'KeySPY.dll';Procedure TKBForm.Icontray(Var Msg:TMessage);
var pt:Tpoint;
begin
if Msg.LParam=wm_lbuttondown then
sethook1Click(Self);if Msg.LParam=Wm_rbuttondown then
begin
GetCursorPos(pt);
SetForegroundWindow(Handle);
PopupMenu1.Popup(Pt.x,Pt.Y);
end;
end;procedure TKBForm.FormCreate(Sender: TObject);
begin
NormalIcon:=Ticon.Create;
Application.Title:=Caption;nid.cbSize:=Sizeof(nil);
nid.Wnd:=Handle;
nid.uID:=0;
nid.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
nid.uCallbackMessage:=MI_iconEvent;
nid.hIcon:=Application.Icon.Handle;
StrCopy(nid.szTip,pchar(Caption));
nid.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;if not Shell_NotifyIcon(nim_add,@nid) then
begin
ShowMessage('失敗');
Application.Terminate;
end;
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
end;procedure TKBForm.SetHook1Click(Sender: TObject);
begin
Setkeyhook;
end;procedure TKBForm.EndHook1Click(Sender: TObject);
begin
Endkeyhook;
end;procedure TKBForm.Close1Click(Sender: TObject);
begin
Application.Terminate;
end;procedure TKBForm.FormDestroy(Sender: TObject);
begin
nid.uFlags:=0;
Shell_NotifyIcon(nim_delete,@nid);
end;
procedure TKBForm.FormPaint(Sender: TObject);
begin
Hide;
end;end.