在.NET中交换鼠标左键和右键

时间:2009-03-17 11:53:28

标签: .net mouse user32

如何在.NET中交换左右鼠标按钮(最好是C#)?基本上结果应该与用户通过控制面板选中鼠标属性中的“切换主按钮和辅助按钮”复选框相同。我正在处理Windows XP,以防万一。

3 个答案:

答案 0 :(得分:13)

您可以使用对SwapMouseButton的Windows API调用:

using System.Runtime.InteropServices;

// ...

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

// ...

// Swap it.
SwapMouseButton(1); 

// Back to normal.
SwapMouseButton(0); 

答案 1 :(得分:3)

这样的事情:

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);

答案 2 :(得分:1)

Here's a code snippet这样做。