# Code Optimism

Window focus stealing has been around for a long time, there's a Wikipedia article on it, it's been asked a few times on SuperUser and there's apparently a ForegroundLockTimeout registry value that may assist. But it probably doesn't.

The fact is that some applications don't respect system settings, and steal focus anyway.

You can probably write code to take your focus back, possibly even fast enough to avoid the thieving app from receiving dangerous input.

AutoHotkey, is, once again, ideally suited, and this problem is certainly under discussion on their forum.

So I stole some last active window code and a bit of UAC elevation code, and we've a means not to stop focus stealing (sorry, haven't given that a go yet) but at least to record a history of focused windows so we can see exactly what process / window it was that stole focus and intercepted our keystroke. What the hell did we just authorize? Well, we can know.

Install AutoHotkey, then download the script. Execute it and you've now a history of windows that have come into focus.

Drop a shortcut to "C:\path\to\AutoHotkey.exe" "C:\path\to\windowHistory.ahk" into your start menu startup folder to always have it running. Now find and double click the new AHK icon in the notification area to open the script window, from the View menu select Variables and their contents. At the bottom you will see the variable winNum, which is the most recent recorded window index.

The last 30 windows and their processes are listed here, saved to these variables in a loop. Go to the one matching the winNum value for the latest and count backward to see the windows in focus prior (after zero you jump back to twenty-nine). The number after process names is the PID or process identifier, you can match this to an exact process in task manager.

This is crude and not much more than a copy paste job, but it demonstrates that we don't have to live with these frustrations, we can take action.