How can I find out which application is stealing focus in Vista?

26
2013-07
  • Bill Reardon

    This question is little different from preventing an application from stealing focus, rather how can I find out what the heck is stealing focus?

    It goes like this. I'll be typing something in Firefox on Vista (as I am now). Every few minutes something else is stealing focus. It's not because a window is moving to the foreground. The change is somewhat subtle in window in the foreground takes on the attributes of a de-selected window (e.g., menus slightly dimmed), but it's most notable when typing, as the keys I type are no longer sent to that window. It also makes it extremely annoying.

    How can I track this bugger down so I can remove it?

  • Answers
  • seeanimal

    Try Sysinternals ProcessExplorer. It's a task manager replacement tool which accurately tracks every tiny little process that pops up.

    Leave it running and return to your app. When the problem occurs alt-tab to Procexp and you may be able to catch what is grabbing all the attention.

    Edit: I have literally just found this: Auslogics TaskManager Which would almost certainly help you identify what's going on, with a more intuitive interface and a wealth of more easily accessible information than Procexp.

  • matmat07

    Maybe you can try to alt+tab and see what is the first icon in the list. If it's a laptop, maybe you are touching the touchpad by mistake and you click on the desktop.

  • Robin Smidsrød

    I have the same problem as the poster. I've tried to Alt-Space to bring up the window menu. This definitly shows that there is a "hidden" window present (0x0 in size) that has focus. Using the move menu option to move it around doesn't reveal what it belongs to. Trying to choose the Close menu option does absolutely nothing. Seems like the program ignores the close window event.

    I was thinking about a debugger which would be able to add a menu to the window menu of all windows that would allow it to attach to a debugger during runtime. Anyone know of a program that does this?

    Update: After it happening again I actually tried the "Minimize" menu option, and loe and behold, it is the AVG Free 9.0 Web Browser window that appears. But why is it hidden and why does it grab focus approx. every 5-10 minutes?

    Update2: It seems like AVG has a fix for the problem. http://forums.avg.com/cz-en/avg-free-forum?sec=thread&act=show&id=41015#post%5F41015

  • gregmac

    I had this problem as well, so annoying. I used that Auslogics TaskManager, and found a process on there (forget what it was called) that was some video player for AIM ads. Google unknown processes .exe's until you find it, then uninstall it via the Control Panel.

    There was one more thing I saw, and it was this Java automatic running thing, you have to go into: Start button, Control Panel, Java, Advanced Tab, Miscellaneous, and UNCHECK "Java Quick Starter"

    I did those two things, and problem so far seems to be completely solved :). Also, make sure you close AIM and any web browsers before you uninstall.

  • Matthew Lock

    When the app steals focus try pressing the windows key to the right of the spacebar (context menu). It might show context menu that might help work out what app is stealing the focus.

    Failing that try alt-F4 which sends the close message to the window.


  • Related Question

    windows 7 - Preventing applications from stealing focus
  • svandragt

    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.


  • Related Answers
  • Simon P Stevens

    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.

  • Oliver Salzburg

    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

  • Tom Wijsman

    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...

  • harrymc

    I believe that some confusion may exist, as there are two ways of "stealing focus" : (1) a window coming to the foreground, and (2) the window receiving keystrokes.

    The problem referred to here is probably the second one, where a windows claims the focus by bringing itself to the foreground - without the user's request or permission.

    The discussion must split here between XP and 7.

    Windows XP

    In XP there is a registry hack that makes XP work the same as Windows 7 in preventing applications from stealing focus :

    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.

    Windows 7

    (The discussion below mostly applies to XP as well.)

    Please understand that there is no way in which Windows can totally block applications from stealing the focus and remain functional. For example, if during a file-copy your anti-virus detected a possible threat and would like to pop-up a window asking you for the action to take, if this window is blocked then you would never understand why the copy never terminates.

    In Windows 7 there is only one modification possible to the behavior of Windows itself, which is to use 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.

    Otherwise, one must detect and neutralize the guilty program : If this is always the same application that is getting the focus, then this application is programmed to take the focus and preventing this may be done by either disabling it from starting with the computer, or use some setting supplied by that application to avoid this behavior.

    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.

    A desperate measure when all else fails, and if you have identified this badly-programmed application, is to minimize it and hope that will not then bring itself to the front. A stronger form of minimization is to the tray by using one of the free products listed in Best Free Application Minimizer.

    Last idea in the order of desperation is to fracture your desktop virtually by using a product such as Desktops or Dexpot, and do your work in another desktop than the default.

  • Ivo Flipse

    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.

  • avirk

    PowerMenu will do waht you want to do in windows 7. I have Windows-7 32 bit so don't know it will work on 64 bit properly or not. But on my version its working like a charm for me. After installing this just right click on the top of window and select the option you want to.

    enter image description here

    And select the Always on Top option and t will do the rest.

    EDIT

    OK I got it!

    You can use these two more programs which are easy to use. Always on Top will work good just you have to launch it after installing it and then just drag the hand button to the desired app to make it on top always and you can toggle it through the Ctrl+F8 and lose focus from active windows you can use Ctrl+Spacebar.

    enter image description here

    Second tool is the Deskpin just install it and then click on the pin icon in the taskbar tray icon and then click it on the windows top which you want to keep on top.