SIGN UP MEMBER LOGIN:    
ARTICLE

Programmatically Swapping Mouse Buttons

Posted by Mohammad Elsheimy Articles | Hardware September 06, 2009
In this article we will learn how to swap mouse buttons programmatically - to make the left button acts as the right and vice versa.
Reader Level:

I previously wrote this article in my blog, Think Big!.

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.jpg

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() which 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 DllImportAttribute attribute defines the DLL that contains the function.

The MarshalAsAttribute attribute 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);
}

Good day!

Login to add your contents and source code to this article
share this article :
post comment
 

abc

Posted by nguyen binh Jul 11, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor