Geeks With Blogs
Igor Milovanović .NET, cats and more...


I am using the mouse with my left hand but I am not swapping the mouse buttons. (Old habit  from the times I was using public workstations at the university. I was too lazy to play with the system setup every time, so that I just moved the mouse from the right to the left side.)

I am also using multiple pointing devices with my notebook. (a gaming mouse with multiple buttons at home, a simpler one at work, and also from time to time the touchpad ). Normally I would setup the mouse button layout in the driver once  for every device . A few months ago however I bought a Roccat Kova gaming mouse. Cool, fast, precise but unfortunately also driverless. Swapping the mouse button layout for Kova and for all other pointing devices started being rather complicated: Click on Control-Panel, Mouse, Swap-Mouse Buttons etc.

After few times I had to do go through the Control Panel, I decided that I had enough of clicking (and I was also in the mood to play around a bit with the technology) so I wrote a small windows 7 gadget in Silverlight which can be used to quickly swap mouse buttons. This way I can just quickly double-click the gadget on the desktop to swap layout, without the need to go to control panel.( I am planning to add some automated device recognition to the gadget in the future, but for now I am pretty much ok with this.)


Swapping the mouse buttons under windows is actually a very simple API call:


    public static class Win32ApiUtilities
        private static readonly int SM_SWAPBUTTON = 23;
        public static bool IsMouseSwapped()
            return GetSystemMetrics(SM_SWAPBUTTON) > 0;
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int GetSystemMetrics(int nIndex);
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool 
            SwapMouseButton([param: MarshalAs(UnmanagedType.Bool)] bool fSwap);


The problem with API-calls is that they can’t be done directly from Silverlight (silverlight runs in a sandbox), however there is a workaround :


A windows 7 gadget is actually a simple html page with some XML settings zipped and renamed to .gadget file. As Silverlight can communicate with the JavaScript engine of the host page, and the security settings of a gadget allow the browser to instantiate and invoke COM objects, the solution for the invocation of an API call from Silverlight can be done like this: Silverlight –> JavaScript –> COM Interop –> API Call. Not nice but doable. Here are the necessary steps:


Create a class and register the assembly for COM Interop:

public class MouseSwapper : IMouseSwapper
    public void LeftySetting()


Write a script function  in the gadget HTML page to instantiate the ActiveXObject and invoke the method:


<script type="text/jscript">
        function LeftySetting() {
            var mouseSwapper = new ActiveXObject("MouseSwapper.MouseSwapper");
And call the script from Silverlight:


This is how the gadget looks like:









If you are left handed and happen to have the same problem like me with your Kova (or any other driverless device) the working version of the gadget is below (You will need .NET Framework 4.0 and Silverlight 4 installed on your machine):


Posted on Friday, October 1, 2010 12:52 AM | Back to top

Comments on this post: Mouse Button Swapper – A simple windows 7 Silverlight gadget

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Igor Milovanovic | Powered by: