tags:

views:

43

answers:

2

Hi,

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?

Thanks,

Mr. Wonko

A: 

You need to setup windows keyboard input hook. Here is an example how to do it http://www.aghausman.net/dotnet/disable-special-keys-in-win-app-c.html

It is even easier to do in C++

Vlad
I first didn't see how this is related to my question but upon further reading it became clear and was actually rather interesting... It's nice what kind of things you can do with hooks :-)
Mr. Wonko
+1  A: 

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
peterchen
Thanks for the info!I looked at SetWindowsHookEx and it seems like a DLL isn't actually necessary. It worked just fine with g_InputHookKeyboard = SetWindowsHookEx(WH_KEYBOARD, InputHookKeyboardProc, NULL, GetCurrentThreadId());in my test application.
Mr. Wonko
It's ok if you hook only events of a thread that belongs to your process. To fetch events from threads of other processes, you nedd the DLL, though.
peterchen