Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping left and right mouse button in .NET

Tags:

.net

mouse

user32

How do I swap left and right mouse buttons in .NET (preferably C#)? Basically the result should be the same as if the user checked the "Switch primary and secondary buttons" checkbox in the Mouse Properties through the control panel. I'm dealing with Windows XP, in case that makes a difference.

like image 446
Eugene Katz Avatar asked Mar 17 '09 11:03

Eugene Katz


People also ask

Can a mouse button be changed from left to right handed?

Right-click the Windows icon and select Search. Type mouse. Select Mouse Settings. Under the Select your primary button drop-down, choose Left or Right.


2 Answers

You can use a Windows API call to SwapMouseButton:

using System.Runtime.InteropServices;

// ...

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

// ...

// Swap it.
SwapMouseButton(1); 

// Back to normal.
SwapMouseButton(0); 
like image 66
John Feminella Avatar answered Sep 22 '22 03:09

John Feminella


Something like this:

using Microsoft.Win32;

var key = Registry.CurrentUser.CreateSubKey("Control Panel\\Mouse\\");
var newValue = key.GetValue("SwapMouseButtons");

if (newValue == null) newValue = "1";
else                  newValue = Int32.Parse(newValue) == 1 ? "0" : "1";

key.SetValue("SwapMouseButtons", newValue, RegistryValueKind.String);
like image 30
porges Avatar answered Sep 22 '22 03:09

porges