I'm using SetWindowsHookEx
to register a global hotkey that is supposed to activate my WPF window and bring it to the front.
I found 3 different functions that all seem to achieve that same goal, Window.Activate
, SwitchToThisWindow
and SetForegroundWindow
. I noticed that all 3 methods of bringing my window in focus behaved as expected when debugging in Visual Studio, but failed to properly activate it when the compiled program is run outside the VS debugger.
I created a small example project to illustrate the issue. The application just displays the Activated/Deactivated
and GotKeyboardFocus/LostKeyboardFocus
events of the window, as well as the last key it captured. It binds the 3 different activation methods to the global hotkeys 1-3
.
Below are two gifs of the application with and without debug mode. Both times I'm switching from a currently focused Notepad window to my application using the global hotkey, and then just pressing some keys.
Both windows receive the Activated
and GotKeyboardFocus
events, but only in debug mode does the application actually receive key presses. The title bar also doesn't change from grey to black when running without debug mode, suggesting the window was never really activated.