Keyboard Shortcut to Swap Mouse Buttons

22

11

I use my mouse with both hands and like to switch back and forth for comfort reasons. However, this is made difficult by needing to go through about a zillion layers of menus to swap the buttons each time. Is there an easy way to create a single keyboard shortcut that would swap my left and right mouse button?

Edit: My OS is Windows 7.

dsimcha

Posted 2010-11-01T18:12:26.100

Reputation: 1 074

Answers

19

As blsub6 mentioned, you can change a registry value (with a command called from a batch file):

REG ADD "HKCU\Control Panel\Mouse" /t REG_SZ /v SwapMouseButtons /d 1 /f

or

REG ADD "HKCU\Control Panel\Mouse" /t REG_SZ /v SwapMouseButtons /d 0 /f

However, you need to logout before it will take effect.

The better solution is to make a tiny .exe with C# to swap the setting, as described in the answers to this question.

Make a text file which you can call swapmouse.cs, containing this:

using System.Runtime.InteropServices;
using System;

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

    static void Main(string[] args)
    {
        int rightButtonIsAlreadyPrimary = SwapMouseButton(1);
        if (rightButtonIsAlreadyPrimary != 0)
        {
            SwapMouseButton(0);  // Make the left mousebutton primary
        }
    }
}

And compile it to swapmouse.exe with this command:

"%SystemRoot%\Microsoft.NET\Framework64\v3.5\csc" swapmouse.cs

Then you just double-click a shortcut to that exe to swap the mouse buttons. It takes effect immediately.

mivk

Posted 2010-11-01T18:12:26.100

Reputation: 2 270

3To start the exe by a keyboard shortcut: 1. Right-click on the exe and select "Create Shortcut" 2. Move the shortcut to "C:\ProgramData\Microsoft\Windows\Start Menu" or the desktop 3. Right click on the shortcut, go to the Shorcut tab and select a shortcut key – Rad – 2016-11-21T16:07:06.930

5

Here's an app for that: http://code.google.com/p/mouseswap/

If you have AutoIt installed, here's the script to run in an au3 file:

#NoTrayIcon

HotKeySet("#a","MouseSwap")

Global $Buttons

While 1
   Sleep(50)
WEnd

Func MouseSwap()
   If $Buttons = 0 Then
      DllCall("user32.dll", "int", "SwapMouseButton", "int", 1)
      $Buttons = 1
      SplashTextOn("","E8",280,180,-1,-1,33,"Wingdings",80)
      Sleep(600)
      SplashOff()
   Else
      DllCall("user32.dll", "int", "SwapMouseButton", "int", 0)
      $Buttons = 0
      SplashTextOn("","8F",280,180,-1,-1,33,"Wingdings",80)
      Sleep(600)
      SplashOff()
   EndIf
EndFunc

Mica

Posted 2010-11-01T18:12:26.100

Reputation: 678

4

The better AHK code:

Run, main.cpl
Send, {Space}{Enter}

I also use mouse with both hands and also have Win7, this code works nice!

Lady3Millennium

Posted 2010-11-01T18:12:26.100

Reputation: 41

This is the best for quicktypers. – Leonid Dworzanski – 2019-06-06T12:25:11.653

3

Keyboard way of switching mouse buttons on Windows Vista (perhaps 7) and above:

  1. Windows Key
  2. type "mouse"
  3. Spacebar
  4. Enter

Yeah, it's 8 key presses but not too bad... I've done it a bunch

Kevin Driedger

Posted 2010-11-01T18:12:26.100

Reputation: 2 255

start="5">

  • ALT+F4 to close mouse settings window
  • < – Przemyslaw Remin – 2018-01-02T12:33:41.483

    2

    This is the Autohotkey version (modified/based on https://github.com/jNizM/AHK_DllCall_WinAPI/blob/master/src/Mouse%20Input%20Functions/SwapMouseButton.ahk).

    ; autohotkey code - mapped to F12
    F12::
        buttonState := DllCall("user32.dll\SwapMouseButton", "UInt", 1)
        if buttonState <> 0
        {
            buttonState := DllCall("user32.dll\SwapMouseButton", "UInt", 0)
        }
    

    This works fine with all Windows (including Windows 10). I usually map it to a hotkey such as "F12" key on my keyboard (using Autohotkey), and I can toggle between left and right mouse button instantly with press of a key. There's no need to muck with loading Control panel or setting registry / rebooting.

    otter.pro

    Posted 2010-11-01T18:12:26.100

    Reputation: 191

    0

    A few good Autohotkey suggestions here, but this one swaps the buttons in Windows directly and gives a popup notification.

    It's a copy of the mouseswap Autoit script mentioned by mica.

    #a::
    if button = 0
        {
        DllCall("SwapMouseButton", "int", 1)
        button = 1
        SplashTextOn, 120, 30, Mouse Button, Left handed
        Sleep 600
        SplashTextOff
        }
    else
        {
        DllCall("SwapMouseButton", "int", 0)
        button = 0
        SplashTextOn, 120, 30, Mouse Button, Right handed
        Sleep 600
        SplashTextOff
        }
    return
    

    A terser alternative without the popup:

    Swapped := DllCall("SwapMouseButton", Int, 0)
    if Swapped = 0
     DllCall("SwapMouseButton", Int, 1)
    

    Iain

    Posted 2010-11-01T18:12:26.100

    Reputation: 4 399

    -1

    As mivk mentioned, this is straight forward and working like a charm. This is what mivk mentioned

    Make a text file which you can call swapmouse.cs, containing this:

    using System.Runtime.InteropServices;
    using System;
    
    class SwapMouse
    {
        [DllImport("user32.dll")]
        public static extern Int32 SwapMouseButton(Int32 bSwap);
    
        static void Main(string[] args)
        {
            int rightButtonIsAlreadyPrimary = SwapMouseButton(1);
            if (rightButtonIsAlreadyPrimary != 0)
            {
                SwapMouseButton(0);  // Make the left mousebutton primary
            }
        }
    }
    

    And compile it to swapmouse.exe with this command:

    "%SystemRoot%\Microsoft.NET\Framework64\v3.5\csc" swapmouse.cs
    

    Now you may create a folder called C:\Program Files\swapmouse and copy the swapmouse.exe to the newly created folder.

    Now create a shortcut for this swapmouse.exe file on your desktop.

    Under the property of the shortcut file, add a shortcut key, in this case, I used "Ctrl + Alt + S" and apply.

    Now everytime you press "Ctrl + Alt + S" the mouse button will be swapped.

    There is no dependency on the mouse to change the mouse button anymore.

    Saad Faruque

    Posted 2010-11-01T18:12:26.100

    Reputation: 1

    1Welcome to Super User! This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. – DavidPostill – 2016-02-28T18:30:31.073

    -1

    I dunno about a keyboard shortcut but you can make two reg files that do what's described here. Just click and away you go.

    If you really want to get nuts with it, set up an AutoHotkey script that triggers the reg files

    blsub6

    Posted 2010-11-01T18:12:26.100

    Reputation: 867

    That’s no good, simply changing the registry entry has no effect; the buttons remain unchanged. You would need to reboot or log out and back in for them to take effect. – Synetech – 2014-01-05T05:56:00.107

    -1

    Have you looked at: https://www.eithermouse.com/

    This is a compiled autohotkey script that allows you to swap buttons from your sys tray, and also allows you to have two mice with simultaneously. I have two mice which I use to alternate between left and right hands. And this automatically changes angle of cursor and mouse button.

    jetpilot

    Posted 2010-11-01T18:12:26.100

    Reputation: 1

    This would be more helpful if you had included a script to do it. Otherwise this is better as a comment. – Synetech – 2014-01-05T05:56:42.860

    -1

    I like to avoid using random exe files when possible, here is a AutoHotKey solution based on the keyboard solution previously mentioned.

    Make a shortcut at called c:\mouse.ink that opens the mouse settings.

    Use this AHK script:

    #a::
    Run c:\mouse
    sleep 250
    send {Space}
    Send {Enter}
    return
    

    Bill

    Posted 2010-11-01T18:12:26.100

    Reputation: 7

    The exe is just a compiled AutoIt script (similar to AutoHotkey). Instead of the exe, you could run the au3. – Mica – 2014-04-12T05:46:23.230