如何检测鼠标点击?
如何在 Windows 上检测鼠标点击? (XP/Vista/7)。 例如,当我的应用程序运行时,它将检测用户是否单击某些内容(不是在该应用程序 UI 上,而是在 Windows UI 上)。如果是,则执行另一个进程。
我不知道这是否可行,希望有人能给我一些指导。
谢谢!
how can i detect mouse clicks on Windows ? (XP/Vista/7).
for example when my application is running , it will detect if user click on something (not on that application UI but on Windows UI). if yes , execute another process.
I don't know if this is possible , i am hoping someone can give me some guidance.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想拦截任何鼠标点击、移动、鼠标滚轮点击等,你需要编写一个鼠标钩子。
如果您想在自己的应用程序之外跟踪鼠标活动,据我所知,这是唯一的方法。如果要安装挂钩,则需要从 User32.dll 文件导入 SetWindowsHookEx(...) 函数。它涉及互操作 (PInvoke),并且您必须导入 (DllImport) 一些函数。
以下是 Microsoft 的官方文档,介绍了如何在 C# 中实现此目的:
How to set a Windows hook in Visual C# .NET
我将在这里总结一下,只是为了在链接有一天失效时完成答案。
从 SetWindowsHookEx 函数开始:
现在您可以设置您的挂钩。例如:
事后不要忘记将其解开。为此,您将需要另一个 DllImport:
您可以使用 HookProc 委托 (MouseHookProcedure) 来捕获鼠标活动。这涉及一些编组以捕获数据。
不要忘记随后调用钩子链中的下一个项目(CallNextHookEx)!
PS:你可以对键盘做同样的事情。
You need to write a mouse hook if you want to intercept any mouse clicks, movement, mouse wheel clicks...etc.
This is the only way AFAIK if you want to track mouse activity outside of your own application. You need to import the SetWindowsHookEx(...) function from the User32.dll file if you want to install a hook. It involves interop (PInvoke) and you'll have to import (DllImport) some functions.
Here's an official document by Microsoft on how to achieve this in C#:
How to set a Windows hook in Visual C# .NET
I'll summarize it here, just to be complete the answer should the link die one day.
Starting with the SetWindowsHookEx function:
Now you can setup your hook. For example:
Don't forget to unhook it afterwards. You'll need another DllImport for this:
You can use the HookProc delegate (MouseHookProcedure) to capture the mouse activity. This involves some marshalling in order to capture the data.
Don't forget to call the next item in the hook chain afterwards (CallNextHookEx)!
PS: You can do the same for the keyboard.
Windows 挂钩机制 是您所需要的与. 一起工作。看一下这篇文章:在 C# 中处理全局鼠标和键盘挂钩
Windows hooking mechanism is what you need to work with. Take a look at this article: Processing Global Mouse and Keyboard Hooks in C#
虽然 Christophe Geers 解决方案可以帮助您捕获鼠标事件,但它并没有提供问题的完整解决方案。
爱德华想知道如何获取点击事件。
要获取点击事件,请使用 Christophe Geers 提供的解决方案。
并添加/编辑以下内容:
While Christophe Geers solution helps you to capture the mouse event, it does not provide a complete solution to the issue.
Edward wanted to know how to get the click event.
To get the click event, use the solution provided by Christophe Geers.
And add/edit the following: