Click here to Skip to main content
14,601,385 members

Small Utility to Swap Mouse Buttons

Rate this:
5.00 (4 votes)
Please Sign up or sign in to vote.
5.00 (4 votes)
29 Feb 2020CPOL
Toggle mouse button swapping between on and off
This utility toggles mouse button swapping between on and off. If your swapping is initially on, it is disabled and vice versa. You can also use this utility with a true or false startup argument to set the desired setup. You can place the utility on the desktop or some other convenient place.

Introduction

If you're using left-handed mouse buttons, you may have noticed that mouse buttons are often swapped again if you remotely connect to a workstation where you also have left-handed mouse button settings. In other words, since both the remote and local desktop session swap the buttons, the result is right-handed.

You can, of course, open Control Panel and change the setting or modify registry to achieve the desired setup. However, both solutions are cumbersome if you need to change the setup quickly.

This small utility changes the swapping between on and off. If your swapping is initially on, it is disabled and vice versa. You can also use this utility with a true or false startup argument to set the desired setup. You can place the utility on the Desktop or some other convenient place.

The Code

Let's have a quick look at the code:

/// <summary>
/// Nonzero if the meanings of the left and right mouse buttons are swapped; otherwise, 0.
/// </summary>
public const Int32 SM_SWAPBUTTON = 23;

/// <summary>
/// Reverses or restores the meaning of the left and right mouse buttons.
/// </summary>
/// <param name="bSwap">If this parameter is TRUE, 
/// the left button generates right-button messages 
/// and the right button generates left-button messages. 
/// If this parameter is FALSE, the buttons are restored to their original meanings.
/// </param>
/// <seealso cref=

/// "https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-swapmousebutton"/>
/// <returns>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.
/// </returns>
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern Int32 SwapMouseButton(Int32 bSwap);

/// <summary>
/// Determines whether a key is up or down at the time the function is called, 
/// and whether the key was pressed after a previous call to GetAsyncKeyState.
/// </summary>
/// <param name="nIndex">The virtual-key code.</param>
/// <seealso cref=

/// "https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics"/>
/// <returns>If the function succeeds, 
/// the return value is the requested system metric or configuration setting.
/// If the function fails, the return value is 0. 
/// GetLastError does not provide extended error information.</returns>
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern Int32 GetSystemMetrics(Int32 nIndex);

/// <summary>
/// Main method
/// </summary>
static void Main(string[] args) {
   bool swap;

   // Check the arguments
   if (args.Length > 1) {
      PrintSyntax();
      return;
   } else if (args.Length == 1) {
      if (int.TryParse(args[0], out int intArg)) {
         swap = intArg == 1;
      } else if (args[0].ToString().ToLower() == "true") {
         swap = true;
      } else if (args[0].ToString().ToLower() == "false") {
         swap = false;
      } else {
         PrintSyntax();
         return;
      }
   } else {
      // If no arguments are used, get the current setting
      swap = GetSystemMetrics(SM_SWAPBUTTON) == 0;
   }

   // Set the desired state of button swapping
   SwapMouseButton(swap ? 1 : 0);
}

/// <summary>
/// Print the syntax to the console
/// </summary>
static void PrintSyntax() {
   System.Console.WriteLine("Toggle mouse button swapping on or off.");
   System.Console.WriteLine();
   System.Console.WriteLine("ToggleMouseButtonSwapping [true/false]");
   System.Console.WriteLine();
   System.Console.WriteLine("Press any key to continue...");
   System.Console.ReadKey();
}

First, the code checks if arguments are used. If there are too many arguments or the arguments cannot be interpreted, the syntax is displayed on the console. With arguments, the swapping will be set to the desired state.

If no arguments are provided, GetSystemMetrics call is used to get the current state of the swapping so that the state will be toggled to the opposite state.

And last SwapMouseButton call is used to carried out the actual change.

References

Few references you might find useful are:

History

  • 29th February 2020: Created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Wendelius
Architect
Europe Europe
Biography provided

Comments and Discussions

 
QuestionAn Easily Way Pin
ISDHN2-Mar-20 17:34
MemberISDHN2-Mar-20 17:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 29 Feb 2020

Tagged as

Stats

10.7K views
34 downloads