This is not possible without extensive manipulation of Windows internals and you need to get over it.
There are moments in daily computer use when it is really important that you make one action before the operating system allows you to do another. To do that, it needs to lock your focus on certain windows. In Windows, control over this behavior is largely left to the developers of the individual programs that you use.
Not every developer makes the right decisions when it comes to this topic.
I know that this is very frustrating and annoying, but you can't have your cake and eat it too. There are probably many cases throughout your daily life where you're perfectly fine with the focus being moved to a certain UI element or an application requesting that the focus remains locked on it. But most applications are somewhat equal when it comes to deciding who is the lead right now and the system can never be perfect.
A while ago I did extensive research on solving this issue once and for all (and failed). The result of my research can be found on the annoyance project page.
The project also includes an application that repeatedly tries to grab focus by calling:
switch( message ) {
case WM_TIMER:
if( hWnd != NULL ) {
// Start off easy
// SetForegroundWindow will not move the window to the foreground,
// but it will invoke FlashWindow internally and, thus, show the
// taskbar.
SetForegroundWindow( hWnd );
// Our application is awesome! It must have your focus!
SetActiveWindow( hWnd );
// Flash that button!
FlashWindow( hWnd, TRUE );
}
break;
As we can see from this snippet, my research was also focused on other aspects of user interface behavior I don't like.
The way I tried to solve this was to load a DLL into every new process and hook the API calls that cause another windows to be activated.
The last part is the easy one, thanks to awesome API hooking libraries out there. I used the very great mhook library:
#include "stdafx.h"
#include "mhook-2.2/mhook-lib/mhook.h"
typedef NTSTATUS( WINAPI* PNT_QUERY_SYSTEM_INFORMATION ) (
__in SYSTEM_INFORMATION_CLASS SystemInformationClass,
__inout PVOID SystemInformation,
__in ULONG SystemInformationLength,
__out_opt PULONG ReturnLength
);
// Originals
PNT_QUERY_SYSTEM_INFORMATION OriginalFlashWindow =
(PNT_QUERY_SYSTEM_INFORMATION)::GetProcAddress(
::GetModuleHandle( L"user32" ), "FlashWindow" );
PNT_QUERY_SYSTEM_INFORMATION OriginalFlashWindowEx =
(PNT_QUERY_SYSTEM_INFORMATION)::GetProcAddress(
::GetModuleHandle( L"user32" ), "FlashWindowEx" );
PNT_QUERY_SYSTEM_INFORMATION OriginalSetForegroundWindow =
(PNT_QUERY_SYSTEM_INFORMATION)::GetProcAddress(
::GetModuleHandle( L"user32" ), "SetForegroundWindow" );
// Hooks
BOOL WINAPI
HookedFlashWindow(
__in HWND hWnd,
__in BOOL bInvert
) {
return 0;
}
BOOL WINAPI
HookedFlashWindowEx(
__in PFLASHWINFO pfwi
) {
return 0;
}
BOOL WINAPI
HookedSetForegroundWindow(
__in HWND hWnd
) {
// Pretend window was brought to foreground
return 1;
}
BOOL APIENTRY
DllMain(
HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {
switch( ul_reason_for_call ) {
case DLL_PROCESS_ATTACH:
Mhook_SetHook( (PVOID*)&OriginalFlashWindow, HookedFlashWindow );
Mhook_SetHook( (PVOID*)&OriginalFlashWindowEx, HookedFlashWindowEx );
Mhook_SetHook( (PVOID*)&OriginalSetForegroundWindow, HookedSetForegroundWindow );
break;
case DLL_PROCESS_DETACH:
Mhook_Unhook( (PVOID*)&OriginalFlashWindow );
Mhook_Unhook( (PVOID*)&OriginalFlashWindowEx );
Mhook_Unhook( (PVOID*)&OriginalSetForegroundWindow );
break;
}
return TRUE;
}
From my tests back then, this worked great. Except for the part of loading the DLL into every new process. As one might imagine, that's nothing to take too lightly. I used the AppInit_DLLs approach back then (which is simply not sufficient).
Basically, this works great. But I never found the time to write something that properly injects my DLL into new processes. And the time invested in this largely overshadows the annoyance the focus stealing causes me.
In addition to the DLL injection problem, there is also a focus-stealing method which I didn't cover in the implementation on Google Code. A co-worker actually did some additional research and covered that method. The problem was discussed on SO: https://stackoverflow.com/questions/7430864/windows-7-prevent-application-from-losing-focus
The Java installer is an example. I can be typing into my IM window whilst the Java installer is running, and suddenly my keyboard focus is whisked away as Java's command-line opens the command prompt box. Other installers do this too, they can't just run in the background (yes, I know, they always say "close all other windows first" - but there's no reason to do that - I believe this requirement is a relic of XP or earlier). – bgmCoder – 2016-08-08T15:22:38.483
3This is actually way more than annoying, I'd say that it's a security risk. There's nothing to stop an application popping up when you're in the middle of typing a password and grabbing your input. – Chris Peacock – 2016-11-25T16:55:04.893
1@Synetech - yes, especially if it's a "Restart Windows now to apply updates" prompt, which then happens without confirmation and with no opportunity to cancel. – Chris Peacock – 2016-11-25T16:58:21.320
Vista or XP? Since Vista has some known issues with workarounds – Ivo Flipse – 2009-08-05T09:14:12.187
9@Ivo Windows 7 in my case but I think for SuperUser all windows versions would be relevant – svandragt – 2009-08-05T09:48:52.040
For the record, this stealing-focus nonsense started sometime around Windows 9x for me. I recall that back in Windows 3.x this never happened. A program would "blink" showing that some activity happened in it and requires your attention, but it would never steal the windows focus. – pmdci – 2019-01-17T14:36:35.753
In Windows 7, if you know the name of the offending (focus-stealing) application, does that make it any easier to prevent it from stealing focus? – RockPaperLizard – 2019-04-10T02:36:18.437
XP has a known solution, and a question http://superuser.com/questions/138648/how-to-disable-auto-focus-of-opened-windows-apps so maybe this question should be for Windows 7 only?
– endolith – 2011-02-20T01:02:17.8033
The moderator merged this question: http://superuser.com/questions/199821/windows-7-disable-applications-stealing-focus with the current one. This is wrong, the answer to the current question does not apply to windows 7, so it shouldnt be merged. So far I could not find a solution to this problem in Windows 7
– Alex Angelico – 2011-10-13T13:38:58.400@AlejandroAngelico: Since all the answers here are for Windows-XP, the questions should be split and the question re-tagged – endolith – 2012-03-21T19:47:39.177
@sathya... as a reference this forum states that there is no way. However I was unable to locate the blog post mentioned there.
– James Mertz – 2012-03-27T03:51:40.62717This is one of my number one pet peeves with every GUI I have ever used. You're typing and blam, some bleeping dialog box steals focus and half your keystrokes go somewhere else. You'd think that the implementors of windowing systems would have figured out this one out decades ago. If there is activity in a window, delay the exposure of the new window. E.g. don't pop anything up on the GUI until three or four seconds since the last button click or keystroke in the currently focused window. Doh! – Kaz – 2012-03-27T22:15:19.230
1i found when I have my my external hard drive (made by Seagate) or my ipod nano (ahem, apple) connected to my Windows 7 machine, it would appear the "desktop" would steal focus every 30 seconds or so, from what ever i was browsing, be it itunes music, Chrome search results or firefox emails. I turned off the autoplay feature, and that helped for a while, but the problem came back even after autoplay was disabled. I guess i have to keep my external HD and flash drives disconnected for the most part, which sucks cuz thats where all my music is :( Its a really annoying bug that makes me want to S – None – 2012-04-28T17:37:48.537
Also see really lengthy discussion about this issue here.
– Karan – 2013-03-07T03:24:30.85724
This is especially annoying when I'm starting an application, switch to do something else and the new application starts receiving half a sentence of text.
It’s even more annoying when a dialog pops up and you unintentionally dismiss it without even seeing the message because you happened to pressSpace
orEnter
while typing a sentence. – Synetech – 2014-01-01T16:17:27.643