guidox / PC

Stop a Program from Stealing Focus

TL;DR

Raise **ForegroundLockTimeout** to 200000 ms, find the offender with **Get-Process** and its window title, then stop the app's own auto-launch or update check.

Before you start

Requirements

  • Windows 11
  • Admin rights for the machine-wide value
  • 15 minutes
Step by step

Instructions

01

Identify the window that steals focus

The window that appeared last owns the foreground. Logging the foreground window over a minute names the app instead of guessing.

Add-Type @'
using System; using System.Runtime.InteropServices;
public class Fg { [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] public static extern int GetWindowText(IntPtr h, System.Text.StringBuilder s, int n); }
'@
1..10 | ForEach-Object { $b = New-Object System.Text.StringBuilder 256; [Fg]::GetWindowText([Fg]::GetForegroundWindow(), $b, 256) | Out-Null; "{0} {1}" -f (Get-Date -Format HH:mm:ss), $b.ToString(); Start-Sleep -Seconds 6 }
02

Read the foreground lock settings

**ForegroundLockTimeout** decides how long a background process must wait before it may take the foreground. The default is 200000 ms, and a smaller value makes stealing easier.

Get-ItemProperty 'HKCU:\Control Panel\Desktop' | Select-Object ForegroundLockTimeout,ForegroundFlashCount
Get-ItemProperty 'HKCU:\Control Panel\Desktop' | Select-Object * -ExcludeProperty PS*
03

Raise the timeout

A long timeout makes Windows flash the taskbar button instead of switching. The value is in milliseconds and needs a sign-out to take full effect.

Set-ItemProperty 'HKCU:\Control Panel\Desktop' -Name ForegroundLockTimeout -Value 200000 -Type DWord
Set-ItemProperty 'HKCU:\Control Panel\Desktop' -Name ForegroundFlashCount -Value 3 -Type DWord
Get-ItemProperty 'HKCU:\Control Panel\Desktop' | Select-Object ForegroundLockTimeout,ForegroundFlashCount
04

Disable the app's own behaviour

Most focus steals come from an updater or a launcher starting at logon. Removing that entry removes the cause, which is more reliable than any timeout.

Get-CimInstance Win32_StartupCommand | Select-Object Name,Command,Location
Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run' | Select-Object * -ExcludeProperty PS*
Get-ScheduledTask | Where-Object { $_.Triggers.CimClass.CimClassName -contains 'MSFT_TaskLogonTrigger' } | Select-Object TaskName,State
05

Check for a topmost window

An app that sets a topmost window stays visible over everything without stealing focus in the normal sense. Its window style is visible in the window list.

Get-Process | Where-Object MainWindowTitle | Select-Object Name,Id,MainWindowTitle
Get-Process -Id (Get-Process | Where-Object MainWindowTitle | Select-Object -First 1).Id | Select-Object Name,MainWindowHandle
06

Verify over a work session

The test is a full hour of typing without an interruption. If a window still grabs focus, log it again and check the app's update settings rather than Windows.

Get-ItemProperty 'HKCU:\Control Panel\Desktop' | Select-Object ForegroundLockTimeout
Get-Process | Sort-Object StartTime -Descending | Select-Object -First 5 Name,Id,StartTime
Get-WinEvent -LogName Application -MaxEvents 30 | Where-Object LevelDisplayName -eq 'Information' | Select-Object TimeCreated,ProviderName -First 5
Troubleshooting

Common issues

The registry value reverts

A policy or a tweak tool resets it. Check HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System for the same name.

Windows still switches windows

The app may call a different API that bypasses the lock, or it runs elevated, which is exempt. Remove its startup entry instead.

The taskbar flashes constantly

ForegroundFlashCount is high. Set it to 1 or 3 to reduce the flashing while keeping the notification.

Only one application is affected

That app has its own bring-to-front setting. Check its preferences for update notifications or a splash screen.

FAQ

Frequently asked

Is this the same as focus assist?

No. Focus assist suppresses notifications; the foreground lock stops windows from taking the front. Both can help, for different symptoms.

Does a higher timeout break anything?

Legitimate apps flash instead of switching. That is the intended behaviour and nothing else depends on the value.

Why does an elevated app ignore it?

Elevated processes are exempt from the foreground lock by design, so the only fix is to stop the app from launching.

How to undo this

Set ForegroundLockTimeout back to 200000 or 0 and ForegroundFlashCount to 3, then sign out. Re-enable any startup entry you disabled.

Changelog · 1 entry
  1. 2026-11-01
    First version
Interactive version: checklist, feedback, related guides →

Free · no account needed · tracks your progress