Raymond has blogged about how programs can get/steal the "foreground love" by using RegisterHotkey
, which, when invoked, will transfer the foreground-ness to your application.
Attempts to do this manually fail miserably (e.g. using SetForegroundWindow
, SwitchToWindow
, etc.), because applications must not be able to steal the focus from a user (so that keypresses don't go to the wrong place).
The trouble is, today I noticed something weird:
I try to Safely Remove an external drive.
There is a ~7-second pause.
During the pause, I am vigorously typing inside a window.
Suddenly, a message box steals the foreground-ness from my app, and my typing goes into the message box instead.
Clearly, this isn't using a hotkey mechanism -- and yet, Windows was able to steal the focus from my app.
I really doubt that there is anything like a "backdoor" being used just for this particular purpose (though please correct me if I'm wrong), so, assuming that isn't the case, there must be a way to do this correctly, without using a hotkey mechanism.
So the question is, how is this accomplished?
Note:
Hans noted that the "backdoor" is AttachInputThread
, but I'm not really convinced that's what's happening here -- especially since Raymond says that method can cause hangs. Ideas?