Swapping left and right mouse button in .NET

前端 未结 3 1130
遇见更好的自我
遇见更好的自我 2020-12-30 12:46

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

相关标签:
3条回答
  • 2020-12-30 13:35

    Here's a code snippet that does this.

    0 讨论(0)
  • 2020-12-30 13:37

    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);
    
    0 讨论(0)
  • 2020-12-30 13:41

    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); 
    
    0 讨论(0)
提交回复
热议问题