Skip to content

Instantly share code, notes, and snippets.

@psignoret
Last active November 26, 2022 22:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save psignoret/32cecc507fba2508d05f4672fe52eeb2 to your computer and use it in GitHub Desktop.
Save psignoret/32cecc507fba2508d05f4672fe52eeb2 to your computer and use it in GitHub Desktop.
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
}
@julenwang
Copy link

Thanks !

@noveltomatillo8
Copy link

Thanks for putting this together, but would you mind ELI5? Does this need to run in Powershell in the background all the time, or is there another way to apply this fix permanently? Opening in Powershell gives me the blinking cursor and doesn't seem to affect Barrier.

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