Mr. Wonko
Mr. Wonko

Reputation: 730

Getting input if the window is not active (Windows)

Short version:

How can I receive input messages in Windows with C++/C when the window is not active?

Background information:

I'm currently working on an Input System that should not depend on any window, so it can e.g. be used in the console as well.

My idea is to create an invisible window only receiving messages, which is possible using HWND_MESSAGE as hWndParent. It only receives input messages when it's active though, and I don't want this. It should always receive input (unless the application requests it no longer does so, e.g. because it lost focus).

I know this is possible somehow, many applications support global shortcuts (e.g. media players (playback control) or instant messengers (opening the contact list)), I just don't know how. Do you know?

Upvotes: 4

Views: 4522

Answers (2)

Vlad
Vlad

Reputation: 9481

You need to setup windows keyboard input hook. Here is an example how to do it; it is even easier to do in C++

Upvotes: 1

peterchen
peterchen

Reputation: 41126

Options:

  • RegisterHotKey if you need to register just one or a few hotkeys
  • SetWindowsHookEx with WH_KEYBOARD / WH_KEYBOARD_LL. Use when you need to filter many or all keyboard events. However, the hook code needs to be implemented in a DLL (which is loaded into other processes). You need separate 32 bit and 64 bit versions of the DLL

Upvotes: 2

Related Questions