Swapping left and right mouse button in .NET

2019-01-22 23:31发布

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.

3条回答
淡お忘
2楼-- · 2019-01-22 23:48
beautiful°
3楼-- · 2019-01-22 23:49

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); 
查看更多
男人必须洒脱
4楼-- · 2019-01-22 23:55

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);
查看更多
登录 后发表回答