2

I've tried

RUNDLL32.EXE USER32.DLL,SwapMouseButton

But it doesn't work, even when run as administrator.

My goal is to make a .bat file that I can call via my fancy shmancy new mouse I bought that allows me to call any arbitrary command from a mouse click.

Jonathan
  • 1,782

4 Answers4

2

Swap mouse buttons from command line

We can swap mouse buttons by editing the registry value SwapMouseButtons under the registry key HKEY_CURRENT_USER\Control Panel\Mouse. To swap mouse buttons we need to set its value to 1.

Same thing can be done from command line using the below command.

reg add "HKEY_CURRENT_USER\Control Panel\Mouse" /v SwapMouseButtons /t REG_SZ /d 1

It requires a logoff or reboot to make the changes effective.

Reference Swap mouse buttons from command line:


Alternative Solution

How do I use Rundll32 to swapmousebutton? for a C# solution (requires the .NET Framework Runtime to be installed)

DavidPostill
  • 162,382
1

As others already reported, the solution with Windows registry is not so convenient because it's needed to logoff or reboot to get effective changes.

I found this C# solution here : https://pimaxplus.com/en/blog/windows/swapping-mouse-buttons/

Immediatly effective, no logoff/reboot required. Tested and working on Windows 11.

C# code (SwapMouseButtons.cs) :

using System; 
using System.Runtime.InteropServices;

class SwapMouseButtons { [DllImport("user32.dll")] public static extern Int32 SwapMouseButton(Int32 fSwap);

static void Main(string[] args)
{
    // Try to make the right mouse button primary.
    int alreadyPrimary = SwapMouseButton(1);

    // If the meaning of the mouse buttons was reversed previously, 
    // before the function was called, the return value is nonzero.
    // If the meaning of the mouse buttons was not reversed, the 
    // return value is zero.
    if (alreadyPrimary != 0)
    {
        // Make the left mouse button primary.
        SwapMouseButton(0); 
    }
}

}

And compile to get an executable file (SwapMouseButtons.exe) :

"%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\csc" SwapMouseButtons.cs

Then I can call the little .exe from my .bat script. Happy.

Sephi
  • 11
0

I used the text below on windows 10 and saved it as sandbox_mouse_button_swap.wsb

Double-clicking that file opens up the sandbox with the mouse buttons swapped, no reboot required.

<Configuration>
  <LogonCommand>
    <Command>rundll32.exe user32.dll,SwapMouseButton</Command>
  </LogonCommand>
</Configuration>
jt-pdx
  • 1
0

I am no expert at this - I tried both the above solutions but it did not work and so I simply created a .bat file with the following command script, saved it to my Desktop and whenever I want to swap, I double-click the script to make it run and it does the job for me.

rundll32.exe user32.dll,SwapMouseButton

Vinny
  • 1