Take the 2-minute tour ×

Is there a way to track which window currently has keyboard focus. I could handle WM_SETFOCUS for every window but I'm wondering if there's an alternative, simpler method (i.e. a single message handler somewhere).

I could use OnIdle() in MFC and call GetFocus() but that seems a little hacky.

share|improve this question

7 Answers 7

So from the way you worded the question I'm inferring that you want to have an event handler which is invoked whenever focus switches between windows. You want to be notified, rather than having to poll.

I actually don't think calling GetFocus from OnIdle is that much of a hack - sure it's polling, but it's low-overhead polling without side effects - but if you really want to track this, Windows Hooks are probably your best choice. Specifically you can install a CBT hook (WH_CBT) and listen for the HCBT_SETFOCUS notification.

Windows calls the WH_CBT hook with this hook code when Windows is about to set the focus to any window. In the case of thread-specific hooks, the window must belong to the thread. If the filter function returns TRUE, the focus does not change.

You could also do with with a WH_CALLWNDPROC hook and listen for the WM_SETFOCUS message.

Depending on whether you make it a global hook, or app-local, you can track focus across all windows on the system, or only the windows owned by your process.

share|improve this answer
This is the correct answer. –  Ted Middleton May 2 at 1:56

How about the Win32 GetForegroundWindow?

share|improve this answer
I know how to get the window with focus, I'm looking for a way to be notified when the focus changes, in a single location. –  jmatthias Sep 4 '08 at 17:57

There is an easy way using .Net Framework 3.5 : the library UI Automation provides an event focus changed that fires every time the focus change to a new control.

Page on MSDN


public void SubscribeToFocusChange()
    AutomationFocusChangedEventHandler focusHandler 
       = new AutomationFocusChangedEventHandler(OnFocusChanged);

private void OnFocusChanged(object src, AutomationFocusChangedEventArgs e)
    AutomationElement focusedElement = sender as AutomationElement;

This api in fact use windows hook behind the scenes to do that. However you have to use the .Net Framework...

share|improve this answer

If you're programming in .net 3.5, the Automation package olorin mentions is by far the easiest, but beware of using it in a program that itself has a UI, at least if the UI is done in WPF -- the focus tracking hooks get confused by events in its own app, and quickly lock up the UI. I sent MS a bug report on it. I have not observed the same problem using a traditional Windows Forms UI. You could, of course, put the tracking code in a separate console app and use some kind of ipc to transmit the info you need.

The tempting alternative of using Interop to access the WH_CBT Windows Hook from C# won't work -- the only global hooks you can get at from C# are the mouse and keyboard.

share|improve this answer

You could monitor messages for the WM_ACTIVATE event.


share|improve this answer

Well, this may not be very graceful... but you can retrieve the current focused control pretty easily. So you might consider setting up a timer that asks every 1/2 second or so "Where is the current focus?"... Then you can observe changes. Example Delphi code is below; it should be pretty easy to adapt, since the real work is in the Windows API calls.


function TForm1.GetCurrentHandle: integer;
  activeWinHandle: HWND;
  focusedThreadID : DWORD;
  //return the Windows handle of the currently focused control
  Result := 0;
  activeWinHandle := GetForegroundWindow;
  focusedThreadID := GetWindowThreadProcessID(activeWinHandle,nil);
  if AttachThreadInput(GetCurrentThreadID,focusedThreadID,true) then begin
      Result := GetFocus;
      AttachThreadInput(GetCurrentThreadID, focusedThreadID, false);
  end;  //if attached

procedure TForm1.Timer1Timer(Sender: TObject);
  //give notification if the handle changed
  //(this code gets fired by a timer)
  CurrentHandle := GetCurrentHandle;
  if CurrentHandle <> PreviousHandle then begin
    Label1.Caption := 'Last focus change occurred @ ' + DateTimeToStr(Now);
  PreviousHandle := CurrentHandle;

share|improve this answer


Has a window focus tracker sample.

share|improve this answer

Your Answer


