Are there any solutions to prevent applications stealing focus from the active window?

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.

link|improve this question

Vista or XP? Since Vista has some known issues with workarounds – Ivo Flipse Aug 5 '09 at 9:14
4  
@Ivo Windows 7 in my case but I think for SuperUser all windows versions would be relevant – svandragt Aug 5 '09 at 9:48
XP has a known solution, and a question superuser.com/questions/138648/… so maybe this question should be for Windows 7 only? – endolith Feb 20 '11 at 1:02
The moderator merged this question: superuser.com/questions/199821/… 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 – Alejandro Angelico Oct 13 '11 at 13:38
@AlejandroAngelico: Since all the answers here are for Windows-XP, the questions should be split and the question re-tagged – endolith Mar 21 at 19:47
show 3 more comments
feedback

6 Answers

up vote 7 down vote accepted

There is an option in TweakUI which does this. It prevents most of the usual tricks dubious software developers employ to force focus on their app.

It's an ongoing arms war though, so I don't know if it works for everything.

Update: According to EndangeredMassa, TweakUI does not work on Windows 7.

link|improve this answer
1  
is tweakui compatible with windows 7? – frankster Jan 13 '10 at 9:18
@frankster. No idea, sorry, I suspect it probably isn't. Download it and try it. Report back if you do so everyone knows. – Simon P Stevens Jan 13 '10 at 15:51
2  
Even using the registry setting that TweakUI sets doesn't work on Win7. – EndangeredMassa Oct 15 '10 at 19:19
feedback

The standard way to disable applications stealing focus is to :

  1. Use regedit to go to: HKEY_CURRENT_USER\Control Panel\Desktop.
  2. Double-click on ForegroundLockTimeout and set its value in hexadecimal to 30d40.
  3. Press OK and exit regedit.
  4. Reboot your PC for the changes to take effect.

From this point forward, programs should (in theory) no longer steal the focus from the window that you're currently working in. Although this is the documented solution, it may not always work for everybody.

Another possibility is the MS-Windows focus-follows-mouse Registry hacks, where the focus and/or activation goes always to the windows under the cursor. A delay can be added to avoid applications popping up all over the desktop.
See this article : Windows 7 - Mouse Hover Makes Window Active - Enable.

[Note: From the comments below, it seems that this registry hack does not apply any more to Windows 7. So I added another answer for Windows 7.]

link|improve this answer
8  
That is the default setting in W7, and no it still does not work, it is a BUG in Windows, that MS refuses to fix. Focus stealing Drives me nuts too. Some applications steal focus some do not, it is a BUG. – Moab Dec 15 '10 at 16:08
3  
@Moab: It does work, just not for some badly-written applications. That's the registry edit that TweakUI does in XP. – harrymc Dec 15 '10 at 16:40
7  
@Moab: Yes, as always a Microsoft solution is half-baked. – harrymc Dec 15 '10 at 19:06
4  
@harrymc: I still have applications stealing focus. Since this doesn't solve the problem for specific programs or all programs I cannot accept this answer. – Jonathan Hobbs Dec 20 '10 at 8:59
4  
I am using Win7, and my default value for this reg setting is 30d40, and this does not prevent any program from stealing the focus. – Steve Feb 22 '11 at 5:47
show 11 more comments
feedback

In Windows 7, the ForegroundLockTimeout registry entry is no longer checked, you can verify this with Process Monitor. In fact, in Windows 7 they disallow you from changing the foreground window. Go and read about its details, it has even been there since Windows 2000.

However, the documentation sucks and they chase each other and find ways around that.

So, there is something buggy going on with SetForegroundWindow, or similar API functions...

The only way to really do this properly is to make a small application which periodically calls LockSetForegroundWindow, virtually disabling any calls to our buggy API function.

If that's not enough (another buggy API call?) you can go even further and do some API monitoring to see what's going on, and then you simply hook the API calls on every process after which you can get rid of any calls that mess up the foreground. However, ironically, this is discouraged by Microsoft...

link|improve this answer
I'll try to write an application that periodically calls LockSetForegroundWindow, this Friday / Weekend. Given that there are already fixes that work for Windows XP and 7, I don't expect anything to work for Windows 7 in a generic way... – Tom Wijsman Mar 22 at 9:53
Hey Tom, maybe this old project of mine will give you a head start :) code.google.com/p/annoyance – Oliver Salzburg Mar 24 at 9:26
@OliverSalzburg: I know of that. Last line in my post... ^^ – Tom Wijsman Mar 24 at 9:41
1  
Does anyone have a reproducible use case of this in Windows 7? Given that people rather experience the opposite (for example, I often find demanding Windows to be hidden behind my current window) and that I am yet to see this happen in Windows 7, it would be pretty annoying to write an application but being unable to test it. Furthermore, as Microsoft states this should no longer happen with Windows 7. At best people discovered that it could only switch the keyboard's focus by accident, this API call would fix that but I don't know how to test whether it actually works... – Tom Wijsman Mar 24 at 10:47
I have a similar issue which annoys the hell out of me. A rather complex installer that calls other programs during installation (e.g. some service initialization). Typing in another program/window during that last part of the installation is impossible. It's reproducible every time, unfortunately I cannot share the offending application. I'd be willing to test your workaround though. – Daniel Beck Mar 26 at 14:20
show 2 more comments
feedback

A while ago I've done 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: http://stackoverflow.com/questions/7430864/windows-7-prevent-application-from-losing-focus

link|improve this answer
feedback

Ghacks has a possible solution:

It happens several times a day that some applications steal the focus of the active window by popping up. This can happen for a number of reasons, when I extract files or a transfer finishes for instance. It does not matter most of the time when this happens but sometimes I’m writing an article and it does not only mean that I have to type some words again but also that I lost concentration and have to click to regain focus.

The Pro Reviewer website has a tip on how to prevent this from happening. The easiest way of preventing focus stealing is to use Tweak UI which has a setting that is called “Prevent applications from stealing focus”. Checking this option prevents that other applications pop up suddenly and steal the focus of the window you are currently working in.

This only works when the application has been minimized before. Instead of stealing the focus it will flash a number of times which can be defined in the same menu in Tweak UI. If you do not want to use Tweak UI you can change the setting in the Windows Registry.

Navigate to the Registry key HKEY_CURRENT_USER > Control Panel > Desktop and change the ForegroundLockTimeout value to 30d40 (Hexadecimal) or 200000 (Decimal). The key ForeGroundFlashCount defines the amount of flashes of a window to alert the user where 0 means unlimited.

link|improve this answer
8  
This doesn't work on any OS after XP. That registry value is already set to that (by default, I believe) and doesn't work anyway. – EndangeredMassa Oct 15 '10 at 19:18
feedback

Here is another answer, since my previous answer apparently doesn't work for Windows 7.

If this is always the same application that is getting the focus, then this application is programmed to take the focus and there is no way to prevent it if the application itself doesn't provide a setting for stopping this behavior.

One idea you could try is to minimize this badly-programmed application and hope this way to disable this "feature". Or you could try to minimize it instead to the tray by using one of the free products listed in Best Free Application Minimizer.

If you do not know which application is stealing the focus, you could use the vbs script included in VB Code which identifies who's stealing focus, which the author used to identify the culprit as a "call home" updater for a printer software.

Another idea would be to fracture your desktop virtually by using a product such as Desktops or Dexpot, and to do your work in another desktop than the default.

link|improve this answer
1  
Minimized applications that ask for focus will get there focus despite that they are minimized, giving an application focus does not mean it will change it's window state by default because focus and window state are two different concepts. Clicking an application on the task bar changes both. Looking at the application stealing focus is indeed an option, but wouldn't lock it out from stealing focus. As for different desktops, the focus usually works across desktops even if it be for a small amount of time... – Tom Wijsman Mar 22 at 9:33
@TomWijsman: Minimizing will work if the stealing application "cooperates" by not stealing the focus when minimized, which is why I said "try". For the rest, only testing will tell, in view of the poster's installed applications and working habits. – harrymc Mar 22 at 9:39
But then that would not be a generic solution. – Tom Wijsman Mar 22 at 9:49
@TomWijsman: If the stealer is a specific application, then there cannot be any generic solution. – harrymc Mar 22 at 10:07
Of course it can, there is only a limited amount of ways to steal focus. Actually those that are provided by the Windows API, supposing your computer is secure. The days you could mess with memory are gone. – Tom Wijsman Mar 22 at 10:15
feedback

protected by slhck Apr 28 at 19:44

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.