Windows en realidad tiene un indicador para habilitar focus-follows-mouse ("seguimiento de ventana activa"), que se puede habilitar fácilmente a través de la monstruosa llamada Win32 API "SystemParametersInfo" . Existen programas de terceros para habilitar el indicador, como los controles X-Mouse , o puede realizar la llamada directamente con PowerShell.
La documentación no siempre es muy clara sobre cómo pvParamse usa el argumento, y algunos fragmentos de PowerShell pasan incorrectamente un puntero al valor, en lugar del valor en sí mismo, al establecer este indicador en particular. Esto termina siendo siempre interpretado como true, es decir, accidentalmente funcionan para habilitar la bandera, pero no para deshabilitarla nuevamente.
A continuación se muestra un fragmento de PowerShell que realiza la llamada correctamente. También incluye una verificación de errores adecuada, y he intentado ir por la limpieza en lugar de la brevedad, para que también sea más fácil agregar envoltorios para otras funciones SystemParametersInfo, en caso de que encuentre alguno que le interese.
Gracias a pinvoke.net por ser un recurso útil para cosas como esta.
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 )