Call me...

Saturday, February 6, 2021

Swap mouse buttons via Powershell

I'm a southpaw, but I can mouse with either hand...
At home, I have the mouse on the left, at work, on the right... For numerous reasons...

Occasionally, I run into situations where I need to swap mouse buttons on my home computer, when I remote connect to something set up for right-hand

This script will do a quick mouse-button swap. 

Though it includes all of the mechanics needed to interact with this setting, for any other scenario.

Note that: 

Just changing the registry value, will work... But, you'd have to reboot.
Pushing that 'true / false' value, to the '
user32.dll' file, does the swap on-the-fly (it also changes the reg key value).
You may need to adjust that '
1..10', (increase the value past '10', I mean) if the second time the 'MouseReport' runs, and the swap does not show as changed yet... It does take a second or two...

= Add-Type -MemberDefinition @'
public static extern bool SwapMouseButton(bool swap);
-Name "NativeMethods" -Namespace "PInvoke" -PassThru
Function MouseReport {$script:MouseButton = $null; $script:MouseButton = (Get-ItemProperty "HKCU:\Control Panel\Mouse").SwapMouseButtons
If ($MouseButton -eq 0) {Write-Host " Mouse button is set for RIGHT-Handed mouse. " -ForegroundColor Red -BackgroundColor Yellow}
If ($MouseButton -eq 1) {Write-Host " Mouse button is set for LEFT-Handed mouse. " -ForegroundColor Green -BackgroundColor Black}}
    $Choice = @('Y','X'); Do { Write-Host "  Swap?: (Y) / (X): " -NoNewline -ForegroundColor Yellow
    $Swap = (Read-Host).Trim()} Until (($Choice.Contains($Swap.ToUpper()))); If ($Swap -eq "x"){break}
# $true for Left-Handed, $false for Right-Handed.
        # Mouse is Right-Handed, change it to Left-Handed
            If ($MouseButton -eq 0) {[bool]$returnValue = $swapButtons::SwapMouseButton($true)}
# Mouse is Left-Handed, change it to Right-Handed
            If ($MouseButton -eq 1) {[bool]$returnValue = $swapButtons::SwapMouseButton($false)}
1..10 | % { #Give the system time to respond to the above change to the "user32.dll" change.
    If ($_ -lt 10) {Write-Host "." -ForegroundColor Cyan -NoNewline};Start-Sleep -Milliseconds 200
    If ($_ -ge 10) {Write-Host "." -ForegroundColor Cyan}}


  1. Thanks! Here is a non-interactive variation that might be useful to others:

    $userdll = @'
    public static extern bool SwapMouseButton(bool swap);
    $swapButtons = Add-Type -MemberDefinition $userdll -Name "NativeMethods" -Namespace "PInvoke" -PassThru

    $rightButtonIsAlreadyPrimary = $swapButtons::SwapMouseButton(1)
    If ($rightButtonIsAlreadyPrimary -eq 1) {$swapButtons::SwapMouseButton(0)}

  2. This comment has been removed by the author.

  3. For a beginning, you could roll out slight improvements to the work process including undertakings that are clumsy, change all work seats to ergonomic desk seats whenever the situation allows, and you are headed to a superior efficiency and better main concern.