Skip to content

Instantly share code, notes, and snippets.

@scaryrawr
Created February 18, 2021 19:06
Show Gist options
  • Save scaryrawr/6b42ba942c484ff600a2202c3299466c to your computer and use it in GitHub Desktop.
Save scaryrawr/6b42ba942c484ff600a2202c3299466c to your computer and use it in GitHub Desktop.
A script for updating focus behavior on Windows
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[bool]
$FollowMouse,
[Parameter(Mandatory = $true)]
[bool]
$RaiseWindows,
[Parameter(Mandatory = $true)]
[uint]
$TrackDelay
)
Add-Type -TypeDefinition @"
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
public static class Mouse {
[DllImport("User32.dll", SetLastError=true)]
public static extern bool SystemParametersInfo(uint action, uint iParam, UIntPtr pvParam, uint flags);
public static void SetTrackMouse(bool enabled) {
if (!SystemParametersInfo(0x1001, 0, new UIntPtr(enabled ? 1u : 0u), 0x2)) {
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
public static void SetRaiseWindows(bool enabled) {
if (!SystemParametersInfo(0x100D, 0, new UIntPtr(enabled ? 1u : 0u), 0x2)) {
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
public static void SetTrackDelay(uint timeout) {
if (!SystemParametersInfo(0x2003, 0, new UIntPtr(timeout), 0x2)) {
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
}
"@
[Mouse]::SetTrackMouse($FollowMouse)
[Mouse]::SetRaiseWindows($RaiseWindows)
[Mouse]::SetTrackDelay($TrackDelay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment