简体   繁体   English

如何捕捉键盘和鼠标事件?

[英]How to catch Keyboard and mouse events?

I want to create an application. 我想创建一个应用程序。 This application has to do something when a user presses special keys on keyboard or/and uses scroll wheel. 当用户按下键盘上的特殊键或/并使用滚轮时,此应用程序必须执行某些操作。 This application is a service. 此应用程序是一项服务。 It has no windows. 它没有窗户。 I want to catch any keyboard or mouse events which were designed with other applications. 我想抓住任何使用其他应用程序设计的键盘或鼠标事件。

For example, you are watching TV by 3rd party application. 例如,您正在通过第三方应用程序观看电视节目。 If you press Ctrl + Shift and use scroll wheel my application changes the volume. 如果按Ctrl + Shift并使用滚轮,我的应用程序会更改音量。

I use Windows 7 x64 and Visual Studio 2008. 我使用的是Windows 7 x64和Visual Studio 2008。

You can call SetWindowsHookEx() to be notified when various events occur. 您可以调用SetWindowsHookEx()以在发生各种事件时收到通知。 You probably want to use the keyboard hook and the mouse hook to watch for mouse events. 您可能希望使用键盘钩子和鼠标钩来观察鼠标事件。

If your application is a true Win32 service, then on Vista and beyond, the application won't receive keyboard or mouse events - to close a security hole (search for "shatter attack"), Microsoft isolated all services to prevent them from interacting with the user. 如果您的应用程序是真正的Win32服务,那么在Vista及更高版本上,应用程序将不会收到键盘或鼠标事件 - 关闭安全漏洞(搜索“破碎攻击”),Microsoft隔离了所有服务以防止它们与之交互用户。

You'll need to have your application run in the session with the interactively logged on user. 您需要让交互式登录用户在会话中运行您的应用程序。

You can use the RAW INPUT method, It is more reliable than GetAsyncKeyState etc. 您可以使用RAW INPUT方法,它比GetAsyncKeyState等更可靠。

I wrote this article on Code Project that may be of use to you. 我在Code Project上写了这篇文章 ,可能对你有用。

There is both C# and a MASM source code versions available. 有C#和MASM源代码版本可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM