How to Hook the Mouse to Catch Events Outside of your application

本文介绍如何使用Delphi创建全局鼠标钩子来监控用户的鼠标活动,并通过实例演示了当鼠标触碰屏幕边缘时,如何使其移至另一侧的效果。
 
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.

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.


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.

(SCI三维路径规划对比)25年最新五种智能算法优化解决无人机路径巡检三维路径规划对比(灰雁算法真菌算法吕佩尔狐阳光生长研究(Matlab代码实现)内容概要:本文档主要介绍了一项关于无人机三维路径巡检规划的研究,通过对比2025年最新的五种智能优化算法(包括灰雁算法、真菌算法、吕佩尔狐算法、阳光生长算法等),在复杂三维环境中优化无人机巡检路径的技术方案。所有算法均通过Matlab代码实现,并重点围绕路径安全性、效率、能耗和避障能力进行性能对比分析,旨在为无人机在实际巡检任务中的路径规划提供科学依据和技术支持。文档还展示了多个相关科研方向的案例与代码资源,涵盖路径规划、智能优化、无人机控制等多个领域。; 适合人群:具备一定Matlab编程基础,从事无人机路径规划、智能优化算法研究或自动化、控制工程方向的研究生、科研人员及工程技术人员。; 使用场景及目标:① 对比分析新型智能算法在三维复杂环境下无人机路径规划的表现差异;② 为科研项目提供可复现的算法代码与实验基准;③ 支持无人机巡检、灾害监测、电力线路巡查等实际应用场景的路径优化需求; 阅读建议:建议结合文档提供的Matlab代码进行仿真实验,重点关注不同算法在收敛速度、路径长度和避障性能方面的表现差异,同时参考文中列举的其他研究案例拓展思路,提升科研创新能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值