Swapping left and right mouse button in .NET

南笙酒味 提交于 2019-12-18 12:39:05

问题


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.


回答1:


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



回答2:


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



回答3:


Here's a code snippet that does this.



来源:https://stackoverflow.com/questions/653911/swapping-left-and-right-mouse-button-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!