Here is a window showing how to manage the window focus (gain or loss) under Windows (tested on XP and Win 7)
Normaly the events Gain or Loss of focus are not fired when interact with other apps.
With these few lines of code, you can do anything you want using the native Windev events.
Free to use and abuse !
Good devs