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:
public const Int32 SM_SWAPBUTTON = 23;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern Int32 SwapMouseButton(Int32 bSwap);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern Int32 GetSystemMetrics(Int32 nIndex);
static void Main(string[] args) {
bool swap;
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 {
swap = GetSystemMetrics(SM_SWAPBUTTON) == 0;
}
SwapMouseButton(swap ? 1 : 0);
}
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