These settings can only be changed in a running session through the SystemParametersInfo
function. (That's how the mouse settings UI makes its changes.) If you already have a mechanism that writes your desired settings to the Registry, you can follow it up with this PowerShell script to read the Registry entries and apply them with SystemParametersInfo
:
Add-Type @"
using System.Runtime.InteropServices;
public class PInvoke {
[DllImport("user32.dll")] public static extern int SystemParametersInfo(int uiAction, int uiParam, int[] pvParam, int fWinIni);
[DllImport("user32.dll")] public static extern int SystemParametersInfo(int uiAction, int uiParam, System.IntPtr pvParam, int fWinIni);
}
"@
$mouse = Get-ItemProperty 'HKCU:\Control Panel\Mouse'
# DoubleClickHeight -> SPI_SETDOUBLECLKHEIGHT
[PInvoke]::SystemParametersInfo(0x001E, $mouse.DoubleClickHeight, $null, 0)
# DoubleClickSpeed -> SPI_SETDOUBLECLICKTIME
[PInvoke]::SystemParametersInfo(0x0020, $mouse.DoubleClickSpeed, $null, 0)
# DoubleClickWidth -> SPI_SETDOUBLECLKWIDTH
[PInvoke]::SystemParametersInfo(0x001D, $mouse.DoubleClickWidth, $null, 0)
# MouseHoverHeight -> SPI_SETMOUSEHOVERHEIGHT
[PInvoke]::SystemParametersInfo(0x0065, $mouse.MouseHoverHeight, $null, 0)
# MouseHoverTime -> SPI_SETMOUSEHOVERTIME
[PInvoke]::SystemParametersInfo(0x0067, $mouse.MouseHoverTime, $null, 0)
# MouseHoverWidth -> SPI_SETMOUSEHOVERWIDTH
[PInvoke]::SystemParametersInfo(0x0063, $mouse.MouseHoverWidth, $null, 0)
# MouseSensitivity -> SPI_SETMOUSESPEED
[PInvoke]::SystemParametersInfo(0x0071, 0, [IntPtr][int]$mouse.MouseSensitivity, 0)
# MouseThreshold1, MouseThreshold2, MouseSpeed -> SPI_SETMOUSE
[PInvoke]::SystemParametersInfo(0x0004, 0, [int[]]($mouse.MouseThreshold1, $mouse.MouseThreshold2, $mouse.MouseSpeed), 0)
# MouseTrails -> SPI_SETMOUSETRAILS
[PInvoke]::SystemParametersInfo(0x005D, $mouse.MouseTrails, $null, 0)
# SnapToDefaultButton -> SPI_SETSNAPTODEFBUTTON
[PInvoke]::SystemParametersInfo(0x0060, $mouse.SnapToDefaultButton, $null, 0)
# SwapMouseButtons -> SPI_SETMOUSEBUTTONSWAP
[PInvoke]::SystemParametersInfo(0x0021, $mouse.SwapMouseButtons, $null, 2)
First it declares the SystemParametersInfo
native function—twice with slightly different signatures, because the function is strangely inconsistent about how to pass the new setting. Then the script obtains the values of the Mouse
Registry key. For each (except a few, mostly sound-related, that seem vestigial), the script calls the system parameters function with the appropriate action/setting code and input format as listed in the documentation. Each call outputs a 1 if successful. When updating the last setting, the SPIF_SENDCHANGE
flag (2) is passed to notify other applications that changes have been made.
Since the script gets the data from the Registry, I chose not to have it redundantly update the Registry, but if you decide to adapt the script to set specific mouse settings instead of getting them from the Registry, apply SPIF_UPDATEINIFILE
to each SystemParametersInfo
call by changing the last parameter from 0 to 1 or from 2 to 3.
Interestingly, the ActiveWindowTracking
value is also vestigial—the setting is actually stored as part of the UserPreferencesMask
in Desktop—but in case your existing script tried to use the vestigial value, this snippet will apply it for real:
# ActiveWindowTracking -> SPI_SETACTIVEWINDOWTRACKING
[PInvoke]::SystemParametersInfo(0x1001, 0, [IntPtr][int]$mouse.ActiveWindowTracking, 1)
To use the script, save it as a PS1 file, e.g. regmouse.ps1
, then you can call it from a command prompt like so:
powershell -executionpolicy bypass -file regmouse.ps1