Alexey Malistov
Alexey Malistov

Reputation: 26975

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.

Upvotes: 3

Views: 4269

Answers (3)

Mike
Mike

Reputation: 415

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.

Upvotes: 2

Larry Osterman
Larry Osterman

Reputation: 16142

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.

Upvotes: 3

Andy
Andy

Reputation: 30418

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.

Upvotes: 4

Related Questions