How to Hook the Mouse to Catch Events Outside of your application
2006-11-08 21:19
573 查看
From Zarko Gajic,
Your Guide to Delphi Programming.
FREE Newsletter. Sign Up Now!
Learn how to track the mouse activity even when your application is not active, sits in the Tray or does not have any UI at all.
By installing a system wide (or global) mouse hook you can monitor what the user is doing with the mouse and act accordingly.
There are 2 types of hooks - global and local. A local hook monitors things happening only for a specific program (or thread). A global hook monitors the entire system (all threads).
The article "An introduction to hook procedures", states that to create a global hook you need 2 projects, 1 to make the executable file and 1 to make a DLL containing the hook procedure.
zSB(3,3)
Sponsored Links
HL7 and Delphi solutionsEnable your Delphi application to communicate using HL7www.interfaceware.com
Firebird GUI Admin ToolFirebird administration made easy with SQL Manager tool. Try now!www.sqlmanager.net/
IP*Works! Rexec ComponentComponents for VB, .NET, ActiveX, Java, Delphi, Embedded, & more!www.nsoftware.com
Working with keyboard hooks from Delphi explains how to intercept the keyboard input for controls that cannot receive the input focus (like TImage).
Here's an idea for the system-wide mouse hook ....
If, for example, you want to move the mouse to the right side of the screen when it moves toward the left edge (and "touches" it), you might write a global mouse hook to reposition the mouse pointer.
You start by creating a dynamic link library project. The DLL should export two methods : "HookMouse" and "UnHookMouse".
The HookMouse procedure calls the SetWindowsHookEx API passing the "WH_MOUSE" for the first parameter - thus installing a hook procedure that monitors mouse messages. One of the parameters to the SetWindowsHookEx is your callback function Windows will call when there is a mouse message to be processed:
SetWindowsHookEx(WH_MOUSE, @HookProc, HInstance,0) ;
The last parameter (value = 0) in the SetWindowsHookEx defines we are registering a global hook.
The HookProc parses the mouse related messages and sends a custom message ("MouseHookMessage") to our test project:
function HookProc(nCode: Integer; MsgID: WParam; Data: LParam): LResult; stdcall;
var
mousePoint: TPoint;
notifyTestForm : boolean;
MouseDirection : TMouseDirection;
begin
mousePoint := PMouseHookStruct(Data)^.pt;
notifyTestForm := false;
if (mousePoint.X = 0) then
begin
Windows.SetCursorPos(-2 + Screen.Width, mousePoint.y) ;
notifyTestForm := true;
MouseDirection := mdRight;
end;
....
if notifyTestForm then
begin
PostMessage(FindWindow('TMainHookTestForm', nil), MouseHookMessage, MsgID, Integer(MouseDirection)) ;
end;
Result := CallNextHookEx(Hook,nCode,MsgID,Data) ;
end;
Note 1: Read the Win32 SDK Help files to find out about the PMouseHookStruct record and the signature of the HookProc function.
Note 2: a hook function does not need to send anything anywhere - the PostMessage call is used only to indicate that the DLL can comunicate with the "outer" world.
begin
inherited WndProc(Message) ;
if Message.Msg = HookCommon.MouseHookMessage then
begin
//implementation found in the accompanying code
Signal(TMouseDirection(Message.LParam)) ;
end;
end;
Of course, when the form is created (OnCreate) we call the HookMouse procedure from the DLL, when it gets closed (OnDestroy) we call the UnHookMouse procedure.
Download both projects to test and further explore the code.
Note: hooks tend to slow down the system because they increase the amount of processing the system must perform for each message. You should install a hook only when necessary, and remove it as soon as possible.
Your Guide to Delphi Programming.
FREE Newsletter. Sign Up Now!
Learn how to track the mouse activity even when your application is not active, sits in the Tray or does not have any UI at all.
By installing a system wide (or global) mouse hook you can monitor what the user is doing with the mouse and act accordingly.
Windows Hooks?
In short, a hook is a (callback) function you can create as part of a DLL (dynamic link library) or your application to monitor the 'goings on' inside the Windows operating system.There are 2 types of hooks - global and local. A local hook monitors things happening only for a specific program (or thread). A global hook monitors the entire system (all threads).
The article "An introduction to hook procedures", states that to create a global hook you need 2 projects, 1 to make the executable file and 1 to make a DLL containing the hook procedure.
zSB(3,3)
Sponsored Links
HL7 and Delphi solutionsEnable your Delphi application to communicate using HL7www.interfaceware.com
Firebird GUI Admin ToolFirebird administration made easy with SQL Manager tool. Try now!www.sqlmanager.net/
IP*Works! Rexec ComponentComponents for VB, .NET, ActiveX, Java, Delphi, Embedded, & more!www.nsoftware.com
Working with keyboard hooks from Delphi explains how to intercept the keyboard input for controls that cannot receive the input focus (like TImage).
Let's Hook the Mouse ...
By design, the movement of the mouse is restricted by the size of your desktop screen (including the Windows Task Bar). When you move the mouse to the left/right/top/bottom edge, the mouse will "stop" - as expected (if you do not have more that one monitor).Here's an idea for the system-wide mouse hook ....
If, for example, you want to move the mouse to the right side of the screen when it moves toward the left edge (and "touches" it), you might write a global mouse hook to reposition the mouse pointer.
You start by creating a dynamic link library project. The DLL should export two methods : "HookMouse" and "UnHookMouse".
The HookMouse procedure calls the SetWindowsHookEx API passing the "WH_MOUSE" for the first parameter - thus installing a hook procedure that monitors mouse messages. One of the parameters to the SetWindowsHookEx is your callback function Windows will call when there is a mouse message to be processed:
SetWindowsHookEx(WH_MOUSE, @HookProc, HInstance,0) ;
The last parameter (value = 0) in the SetWindowsHookEx defines we are registering a global hook.
The HookProc parses the mouse related messages and sends a custom message ("MouseHookMessage") to our test project:
function HookProc(nCode: Integer; MsgID: WParam; Data: LParam): LResult; stdcall;
var
mousePoint: TPoint;
notifyTestForm : boolean;
MouseDirection : TMouseDirection;
begin
mousePoint := PMouseHookStruct(Data)^.pt;
notifyTestForm := false;
if (mousePoint.X = 0) then
begin
Windows.SetCursorPos(-2 + Screen.Width, mousePoint.y) ;
notifyTestForm := true;
MouseDirection := mdRight;
end;
....
if notifyTestForm then
begin
PostMessage(FindWindow('TMainHookTestForm', nil), MouseHookMessage, MsgID, Integer(MouseDirection)) ;
end;
Result := CallNextHookEx(Hook,nCode,MsgID,Data) ;
end;
Note 1: Read the Win32 SDK Help files to find out about the PMouseHookStruct record and the signature of the HookProc function.
Note 2: a hook function does not need to send anything anywhere - the PostMessage call is used only to indicate that the DLL can comunicate with the "outer" world.
Mouse Hook "Listener"
The "MouseHookMessage" message is posted to our test project - a form named "TMainHookTestForm". We override the WndProc method to get the message and act as needed: procedure TMainHookTestForm.WndProc(var Message: TMessage) ;begin
inherited WndProc(Message) ;
if Message.Msg = HookCommon.MouseHookMessage then
begin
//implementation found in the accompanying code
Signal(TMouseDirection(Message.LParam)) ;
end;
end;
Of course, when the form is created (OnCreate) we call the HookMouse procedure from the DLL, when it gets closed (OnDestroy) we call the UnHookMouse procedure.
Download both projects to test and further explore the code.
Note: hooks tend to slow down the system because they increase the amount of processing the system must perform for each message. You should install a hook only when necessary, and remove it as soon as possible.
相关文章推荐
- How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?
- springBoot启动错误 Your ApplicationContext is unlikely to start due to a @ComponentScan of the default
- How to Enable Trace or Debug for APIs executed as SQL Script Outside of the Applications ?
- Java问题解决:springboot启动出现-Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package
- Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
- Your ApplicationContext is unlikely to start due to a @ComponentScan of the default
- How to Enable Trace or Debug for APIs executed as SQL Script Outside of the Applications ?
- Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package
- 【oneday_onepage】——How to stretch the life of your SSD storage
- How to: How to disable Java Security Warning "The application requires an earlier version of Java."
- How to Avoid Multiple Instances of Your Windows Application
- How to properly use 'dd' to benchmark the write speed of your disk?
- How to Enable Trace or Debug for APIs executed as SQL Script Outside of the Applications ?
- spring boot 报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default p
- spring boot 报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default
- How to: How to disable Java Security Warning "The application requires an earlier version of Java."
- How to invoke the method of managed bean and render view in JSF when we are outside the lifecycle of JSF
- How to Have Two Versions of the Same App on Your Device
- Springboot启动报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default
- You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgrou