Windows actually has a flag to enable focus-follows-mouse ("active window tracking"), which can be enabled easily via the monstrous "SystemParametersInfo" Win32 API call. There are third-party programs to enable the flag, such as X-Mouse Controls, or you can perform the call directly using PowerShell.
The documentation isn't always super clear on how the pvParam argument is used, and some powershell snippets incorrectly pass a pointer to the value, rather than the value itself, when setting this particular flag. This ends up always being interpreted as true, i.e. they accidently work for enabling the flag, but not for disabling it again.
Below is a powershell snippet that performs the call correctly. It also includes proper error-checking, and I've tried to go for cleanliness rather than brevity, to also make it easier to add wrappers for other functionality of SystemParametersInfo, should you find some that interests you.
Shout-out to pinvoke.net for being a helpful resource for stuff like this.
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
public static class Spi {
[System.FlagsAttribute]
private enum Flags : uint {
None = 0x0,
UpdateIniFile = 0x1,
SendChange = 0x2,
}
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SystemParametersInfo(
uint uiAction, uint uiParam, UIntPtr pvParam, Flags flags );
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SystemParametersInfo(
uint uiAction, uint uiParam, out bool pvParam, Flags flags );
private static void check( bool ok ) {
if( ! ok )
throw new Win32Exception( Marshal.GetLastWin32Error() );
}
private static UIntPtr ToUIntPtr( this bool value ) {
return new UIntPtr( value ? 1u : 0u );
}
public static bool GetActiveWindowTracking() {
bool enabled;
check( SystemParametersInfo( 0x1000, 0, out enabled, Flags.None ) );
return enabled;
}
public static void SetActiveWindowTracking( bool enabled ) {
// note: pvParam contains the boolean (cast to void*), not a pointer to it!
check( SystemParametersInfo( 0x1001, 0, enabled.ToUIntPtr(), Flags.SendChange ) );
}
}
'@
# check if mouse-focus is enabled
[Spi]::GetActiveWindowTracking()
# disable mouse-focus (default)
[Spi]::SetActiveWindowTracking( $false )
# enable mouse-focus
[Spi]::SetActiveWindowTracking( $true )