在.NET中交换鼠标左键和右键 | 珊瑚贝

Swapping left and right mouse button in .NET


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

  • 你所说的交换是什么意思…你想为你自己的应用程序进行系统级交换还是交换?


您可以使用 Windows API 调用 SwapMouseButton:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System.Runtime.InteropServices;

// …

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

// …

// Swap it.
SwapMouseButton(1);

// Back to normal.
SwapMouseButton(0);

  • 谢谢。可能要添加您需要”使用 System.Runtime.InteropServices;”
  • 哎呀;我确实忽略了提及这一点。我会添加它以供将来参考。
  • 这有效,但用户注销后状态不会保存。要记住交换状态,您必须将 Porges 的解决方案与注册表一起使用。


类似这样的:

1
2
3
4
5
6
7
8
9
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);


这是执行此操作的代码片段。


来源:https://www.codenong.com/653911/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?