Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adjust the mouse speed when using Barrier (on Windows) to control another computer.
<#
.SYNOPSIS
Adjust the mouse speed when using Barrier (on Windows) to control another computer.
.DESCRIPTION
When using Barrier (https://github.com/debauchee/barrier) across two computers of different
resolutions/DPIs, the server's mouse speed may be too fast or too slow on the client computer:
https://github.com/debauchee/barrier/issues/741. This script monitors Barrier's log file and adjusts
the server's mouse speed when the mouse has moved onto the client.
Inspired in full by Aaron Jensen's (https://github.com/aaronjensen) script at
https://github.com/debauchee/barrier/issues/741#issuecomment-655317497.
.PARAMETER ClientMouseSpeed
The mouse speed to use when on the client computer. 1 is slowest, 20 is fastest
.PARAMETER LogFile
The path to the Barrier log file. Enable logging to file in the Barrier GUI under the
"Barrier" menu item > Change setting. Defaults to "C:\Program Files\Barrier\barrier.log"
.EXAMPLE
.\BarrierMouseSpeedFix.ps1 -ClientMouseSpeed 20
Move the mouse super fast when on the client.
.EXAMPLE
.\BarrierMouseSpeedFix.ps1 -ClientMouseSpeed 1 -LogFile "C:\tmp\barrier.log"
Move the mouse very slowly when on the client, provide a custom log file path.
#>
[CmdletBinding()]
param(
[ValidateRange(1,20)]
[int]$ClientMouseSpeed = 5,
[string]$LogFile = "C:\Program Files\Barrier\barrier.log"
)
if (-not (Test-Path $LogFile -PathType Leaf)) {
throw ("Log file not found at '{0}'" -f $LogFile)
}
$src = @"
public const UInt32 SPI_GETMOUSESPEED = 0x0070;
public const UInt32 SPI_SETMOUSESPEED = 0x0071;
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfoByRef(
uint uiAction, uint uiParam, ref uint pvParam, uint fWinIni);
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfoByVal(
uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
public static uint GetMouseSpeed()
{
uint speed = 0;
SystemParametersInfoByRef(SPI_GETMOUSESPEED, 0, ref speed, 0);
return speed;
}
public static bool SetMouseSpeed(uint speed)
{
return SystemParametersInfoByVal(SPI_SETMOUSESPEED, 0, speed, 0);
}
"@
# If the type exists already (i.e. this script was run multiple times in the same PowerShell
# session), use it. Otherwise (i.e. this is the first time the script is run in the PowerShell
# session), define it to get access to the Win32 APIs.
try {
$User32 = [Win32Functions.User32]
} catch {
$User32 = Add-Type -MemberDefinition $src -Name "User32" -Namespace "Win32Functions" -PassThru
}
$originalMouseSpeed = $user32::GetMouseSpeed()
try {
# Monitor the log file and adjust mouse speed when leaving and entering the server screen
Get-Content $LogFile -Tail 1 -Wait | ForEach-Object {
if ($_.EndsWith("leaving screen")) {
$user32::SetMouseSpeed($ClientMouseSpeed) | Out-Null
} elseif ($_.EndsWith("entering screen")) {
$user32::SetMouseSpeed($originalMouseSpeed) | Out-Null
}
}
} catch {
throw $_.Exception
} finally {
# Reset to original mouse speed, in case anything weird happened
$user32::SetMouseSpeed($originalMouseSpeed) | Out-Null
}
@wangxiujuni
Copy link

wangxiujuni commented Apr 16, 2021

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment