Get KeyStrokes & Mouse without Focus

Started by
0 comments, last by Adam_42 14 years, 1 month ago
Hello I am trying to figure out how to capture the keystrokes & mouse with my Win32 C++ program when the application doesn't have the focus or the app is minimised. I have tried WM_SYSKEYDOWN etc, but I know this doesn't work?
Advertisement
You get no messages (well, not keyboard and mouse, anyway) if you don't have the focus.

If you absolutely need to get to those events, you can install a hook procedure, which is kind of intrusive, and not precisely pretty. Look up SetWindowsHookEx for that. Note that this may cause your program to be flagged as spyware by some antivirus software.

Or, you can use raw input. I've used raw input to read 6dof devices such as space navigator, and it works fine even if the application does not have the focus. I see no reason why it should in principle not work for the mouse or the keyboard identically.
The problem with raw input is that it is, well, raw. That means you lose all configurable and localisable features (ballistics, repeat, keymap) that Windows adds upon these raw messages, you need to do all that yourself, and in the case of keypresses, this will be very painful.
Why do you want to capture input when the app is minimized?

Generally users expect minimized apps and ones without focus not to respond to the mouse or keyboard. If you've just lost focus I think you should still get mouse events when the mouse is over your window though.

There is one standard Windows API for registering global hotkeys that you might find useful, depending on what you're trying to do: RegisterHotKey().

This topic is closed to new replies.

Advertisement