views:

866

answers:

3

I have an application written in .NET. I would like to add the ability for users to control my application by using a mouse, e.g. a high end gaming mouse, with 10-15 buttons on it.

Is this going to be easy to do, or will I need a mouse vendor with a .NET SDK that I can use - can you recommend any?

I would like all control to be within my application and just use the mouse vendors driver.

+1  A: 

Correct me if I am wrong, but don't all these extra buttons on mice normally map to some sort of keyboard combination (often there is a control pannel app that lets you specify which button mapps to what). If this is the case you should be able to capture button four and above using the KeyDown, and KeyUp events.

Martin Brown
That is actually quite a good idea, I will investigate that!
A: 

In windows, whenever you press something, it gets through the win32 message queue. Here is some code to check how to retrieve those messages

Now (you will of course need a 15 buttons mouse), brake point or Console::writeline or whatever inside that code, press a mouse button and bang, you can get the message number which was just pressed.

Afterwards, you could check if you can bind those keypress on normal events handlers.

Note that the solution about controling vendor hardware is not very portable. Suppose that everyone has microsoft mice, but I decide to buy a 20 buttons mouse from lets say Hong-Kong, you'll have to write new code to support that. I think you should aim at very generic solutions

Eric
My users will be using aq specific mouse that I will recommend, so if I can trap all the messages, then that will be sufficient
A: 

You can try to get the Windows messages, for example in WinForms you can override your form's method to get all the messages:

    protected override void WndProc(ref Message msg)
    {
        Console.WriteLine(
          "HWnd:{0}\tLParam:{1}\tWParam:{2}\tMsg:{3}\tResult:{4}",
          msg.HWnd, msg.LParam, msg.WParam, msg.Msg, msg.Result);
        base.WndProc(ref msg);
    }

And play a little bit with your mouse to see what are the messages you have to take.

jmservera
I will be using WPF, but I can use windows forms. Is it best to see if I can trap the messages in winforms, then use p/invoke in WPF?