简体   繁体   中英

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.

I use Windows 7 x64 and Visual Studio 2008.

You can call SetWindowsHookEx() to be notified when various events occur. 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.

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.

I wrote this article on Code Project that may be of use to you.

There is both C# and a MASM source code versions available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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