Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine which mouse was clicked (multiple mice devices) in .NET

Tags:

c#

vb.net

I want to detect when my touchpad is clicked!

I normally use a usb mouse, so I don't use the touchpad for anything. Instead I'd like to make it possible to perform an action in .NET, when the touchpad is clicked. This way I can use it as a shortcut: One tap and something cool happens.

Is this possible, and if yes, any clue how? I'd prefer if it could be working in VB.NET or C#.

My theory is that I'd have to make a mousehook, which then somehow determines which device the click is coming from. If the click is determined to be from the touchpad, then cancel the click and doWhatever().

Thanks!

* EDIT * Well, it's "solved", sort of :) In a odd coincidence, Synaptics released their latest driver and software for their touchpads a few days ago with some new functionality. As my laptop has a synaptics touchpad, I tried out the software and interestingly enough, the functionality for designating clicks on the trackpad to perform an action, was built-in.

So the desired function has been achieved, without a line of code (my own code anyway :).

Answer goes to Adrian though for the link to the RawInputSharp library. I tinkered with it yesterday, and I'm 90% sure it would be possible to use for this purpose, in the event a laptop doesn't have a synaptics trackpad.

like image 869
Morten Kirsbo Avatar asked Mar 20 '10 19:03

Morten Kirsbo


1 Answers

Have a look at the RawInputSharp library from this page. It uses pInvokes into User32.dll to get hold of input device information. Using it you can detect which device (ie. mouse) input is coming from.

Having had a little play with it, I managed to extract some code that displays just the device id - a different value depending on whether I use my USB mouse or my internal touchpad. The tricky thing would be to identify which device id is that of your touchpad automatically, but you could manually configure this in your application.

like image 98
adrianbanks Avatar answered Sep 29 '22 18:09

adrianbanks