Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / Win32

Programmatically Swapping Mouse Buttons

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
11 May 2010CPL2 min read 17.3K   1   4
Swap mouse button using C# and Windows API.

I wrote this article in Arabic too, check it out here

Swapping mouse buttons means swapping the two buttons, making the right button acts like the left one and vice versa. 

This is done -for normal users of course- using the Mouse properties dialog in the control panel. See the next figure.

Swap Mouse Buttons

For we developers you need to do this programmatically, and this is done by a very very trivial -not a joke- Win32 API function, SwapMouseButton (resides on user32.dll). The syntax of the function -in C- is as follows:

BOOL SwapMouseButton(
    BOOL fSwap
);

This function simply takes a single BOOL argument and returns a BOOL value too.
If fSwap is TRUE then the right mouse button will do the primary functions like the left button -by default- does, and the left button will do the secondary functions that the right button -by default- does.
If the function swapped the buttons, then it will return FALSE, otherwise TRUE.

BOOL can take one of values TRUE (non-zero) or FALSE (zero).

In .NET Framework, you need to write a wrapper to this API function and this wrapper is like this:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SwapMouseButton([param: MarshalAs(UnmanagedType.Bool)] bool fSwap);

The DllImport attribute defines the DLL that contains the function.
The MarshalAs attributes defines how .NET types will be mapped to Win32 types (this process called marshaling). We applied this attribute to the return value of the function and to the single argument of the function.
The static extern modifiers are required for any PInvoke function.

PInvoke stands for Platform Invokation. It’s the process of wrapping an API function to .NET code.

Marshaling is the process of creating a bridge between .NET types and unmanaged types.

For unmanaged BOOL, .NET makes the marshaling process automatically, so you can safely remove the MarshalAsAttribute attributes.

Now it’s the time for the code for calling the function.

public void MakeRightButtonPrimary()
{
    SwapMouseButton(true);
}

public void MakeLeftButtonPrimary()
{
    SwapMouseButton(false);
}

Posted in Win32 API Tagged: .NET, API, CodeProject, CSharp

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Technical Lead
Egypt Egypt
Mohammad Elsheimy is a developer, trainer, and technical writer currently hired by one of the leading fintech companies in Middle East, as a technical lead.

Mohammad is a MCP, MCTS, MCPD, MCSA, MCSE, and MCT expertized in Microsoft technologies, data management, analytics, Azure and DevOps solutions. He is also a Project Management Professional (PMP) and a Quranic Readings college (Al-Azhar) graduate specialized in Quranic readings, Islamic legislation, and the Arabic language.

Mohammad was born in Egypt. He loves his machine and his code more than anything else!

Currently, Mohammad runs two blogs: "Just Like [a] Magic" (http://JustLikeAMagic.com) and "مع الدوت نت" (http://WithdDotNet.net), both dedicated for programming and Microsoft technologies.

You can reach Mohammad at elsheimy[at]live[dot]com

Comments and Discussions

 
GeneralMy vote of 1 Pin
Izzet Kerem Kusmezer23-Mar-10 0:32
Izzet Kerem Kusmezer23-Mar-10 0:32 
GeneralRe: My vote of 1 Pin
Mohammad Elsheimy27-Mar-10 4:38
Mohammad Elsheimy27-Mar-10 4:38 
GeneralMy vote of 1 Pin
SledgeHammer0117-Mar-10 16:03
SledgeHammer0117-Mar-10 16:03 
Calling a function is not an article.
GeneralRe: My vote of 1 Pin
Mohammad Elsheimy27-Mar-10 4:38
Mohammad Elsheimy27-Mar-10 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.