Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#
Article

Middle Mouse Button (or Wheel) to Doubleclick (.NET 2.0)

Rate me:
Please Sign up or sign in to vote.
4.62/5 (12 votes)
9 Jul 2007CPOL2 min read 116.2K   1.4K   35   32
This is a small but handy tool I'm using everyday. It converts a middle mouse button click into a left mouse button double click.

UPDATED 18.02.2007

New Usage of SendInput

Now sendInput is called twice (with 1 block sendInput Stuct-Data) instead of once (with 2 struct blocks for double click). Between the two clicks, there is a Thread.Sleep(80) to simulate a delay.

C#
//Send the first click to the same location
UInt32 r = User32.SendInput( ( UInt32 ) input.Length, 
		input, Marshal.SizeOf( input[ 0 ] ) );

//Sleep to simulate delay
System.Threading.Thread.Sleep(80);

//Send second click to the same location
r = User32.SendInput((UInt32)input.Length, input, Marshal.SizeOf(input[0]));

Config GUI

  • Displays function state
  • Enable / disable doubleclick function
  • Choose whether to confirm exit of the application

Sample image

Introduction

Once I had a logitech mouse and everything was fine! Logitech provided you with a handy application called EMEXEC.exe, which would allow you to set up the behavior of the available buttons. The function I most used was the converter for the middle mouse button/wheel which would turn a single click from the specific button into a left mouse button double click. But what if you uninstalled mouseware, or what if you have to work on a machine without LogiMouse. You're not able to install MouseWare until you have a LogiMouse connected. Pretty soon I began to miss this cool feature from Logitech and said to myself that I am going to write a tool which implements exactly this feature. And here we go....

Problem

The trickiest part I was facing was that the application has to get the clicks from all the other applications running at this time. Therefore a global mousehook has to be set and that might be a little bit tricky. But 'good luck' I found a ready made MouseHookManager already written in C#. It's the MouseHookManager class from Olvio which was also available here on The Code Project two years ago. (Thanks to Olvio for putting the source online.)

Setting Up the MouseHook

Setting up the MouseHook is easy with Olvios MouseHookManager. Just follow these steps:

C#
// Here you create a new form and implement the interface IMouseHookUser 
public class Form1 : System.Windows.Forms.Form, 
	Olvio.Windows.Forms.Docking.IMouseHookUser {
//You also have to create an instance of MouseHookManager 
private  Olvio.Windows.Forms.Docking.SafeNativeMethods.MouseHookManager hooker;

... 
//In the constructor of the form goes the following
public Form1() {
    InitializeComponent();
    hooker = new Olvio.Windows.Forms.Docking.SafeNativeMethods.MouseHookManager
		(base.Handle, this);
}

//And your class must implement the MouseHookProc in order to compile successfully
public bool MouseHookProc(int code, int mainParameter, 
		int additionalParameter, Point point, int extraInfo)
{
    if (mainParameter == WM_MBUTTONUP ) {
        doubleClick();
    }
    return false;
}
//That's it for setting up the mousehook - 
//everything else is done for you by the MouseHookManager ;?)

Using the MouseHook

Now after you've setup the mousehook, we can use the MouseHookProc we implemented to handle the mouseevents as needed.

C#
//We already implemented this function, and here we see a call to doubleClick() 
//which is actually our doubleclick generator.
public bool MouseHookProc
    (int code, int mainParameter, int additionalParameter, Point point, int extraInfo)
{
    if (mainParameter == WM_MBUTTONUP ) {
        //Call to our doubleclick generator
        doubleClick();
    }
    return false;
}

Generate a Doubleclick Out of the 'singleclick'

Here you see how to reformat the message and send it back as doubleclick. We use SendInput to send the doubleclick to the control.

C#
public void doubleClick() { 

    Point defPnt = new Point();

    // Call the function and pass the Point, defPnt
    GetCursorPos(ref defPnt);

    // Now after calling the function, defPnt contains the coordinates which we can read
    int nX = defPnt.X;
    int nY = defPnt.Y;

    //Reformat screen coordinates
    Rectangle screen = Screen.PrimaryScreen.Bounds;
    int x2 = ( 65535 * nX ) / screen.Width;
    int y2 = ( 65535 * nY ) / screen.Height;

    //Prepare input structure (down & up)
    Input[] input = new Input[ 2 ];
    //Fill input structure
    addMouseUpDown(ref input, x2, y2);

    //Send the first click to the same location
    UInt32 r = User32.SendInput( ( UInt32 ) input.Length, input, 
			Marshal.SizeOf( input[ 0 ] ) );

    //Sleep to simulate delay
    System.Threading.Thread.Sleep(80);

    //Send second click to the same location
    r = User32.SendInput((UInt32)input.Length, input, Marshal.SizeOf(input[0]));
}

private void addMouseUpDown(ref Input[] input, int x2, int y2) {
    for (int i = 0; i < input.Length; i++) {
        input[i].type = INPUT.MOUSE;
        input[i].mi.dx = x2;
        input[i].mi.dy = y2;
        input[i].mi.dwFlags = MOUSEEVENTF.MOVE | MOUSEEVENTF.ABSOLUTE;

        if ((i % 2) == 0)
            input[i].mi.dwFlags |= MOUSEEVENTF.LEFTDOWN;
        else
             input[i].mi.dwFlags |= MOUSEEVENTF.LEFTUP;
    }
}
...

(Win32 API)

//Win32 function SendInput
[ DllImport( "User32.dll" ) ] public static extern UInt32 SendInput (
    UInt32 nInputs,
    Input[] pInputs,
    Int32 cbSize
);
 
//Structure Input
[ StructLayout( LayoutKind.Explicit ) ] internal struct Input {
    [ FieldOffset( 0 ) ] public int type;
    [ FieldOffset( 4 ) ] public MOUSEINPUT mi;
    [ FieldOffset( 4 ) ] public KEYBDINPUT ki;
    [ FieldOffset( 4 ) ] public HARDWAREINPUT hi;
}

And that's basically what my application MBtn2DblClick does. It waits for any middle mouse button and converts it into a left button doubleclick.

Credits

  • Olvio for his MouseHookManager class

I hope this article is useful to someone and/or helps to understand how mousehooks work. If you are interested in the source of MouseHookManager, I'd also recommend you to download this source since the original location of MouseHookManager is gone and I couldn't find it on the Internet anymore so far. So hope you enjoy this and all the best - hope you'll be reading my next articles as well.

Cheers, Kim

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
programmer and software junkie since 1991 zurich switzerland

Comments and Discussions

 
Question3333 Pin
its.rajeev237-Mar-13 21:32
its.rajeev237-Mar-13 21:32 
QuestionPlease make a conceptual explanation Pin
fvalerin14-Feb-12 6:15
fvalerin14-Feb-12 6:15 
QuestionLeft Click instead of Middle Click? Pin
wizi8312-Jan-10 13:39
wizi8312-Jan-10 13:39 
GeneralMbtn2dblclick Question about registry default Pin
clargar15-Dec-09 8:40
clargar15-Dec-09 8:40 
GeneralNeeds fixing for southpaws Pin
dhstraayer7-Dec-09 8:15
dhstraayer7-Dec-09 8:15 
GeneralWindows 7 - not working in explorer [modified] Pin
owen.ward1-Nov-09 7:11
owen.ward1-Nov-09 7:11 
QuestionNo reaction under XP 32, SP III either Pin
J.W. Roberts3-Jul-09 6:33
J.W. Roberts3-Jul-09 6:33 
GeneralThanks - and a question Pin
Dave Ferrise12-Jan-09 13:04
Dave Ferrise12-Jan-09 13:04 
GeneralRe: Thanks - and a question Pin
Dave Ferrise12-Jan-09 13:14
Dave Ferrise12-Jan-09 13:14 
General1 pixel shift Pin
genh9-Dec-08 5:12
genh9-Dec-08 5:12 
QuestionOther keys?? Pin
arekkusu8223-Apr-08 14:14
arekkusu8223-Apr-08 14:14 
GeneralVista x86 Pin
CyberZeus11-Feb-08 1:10
CyberZeus11-Feb-08 1:10 
GeneralLeftclick simulation Pin
fuelairmix27-Nov-07 6:45
fuelairmix27-Nov-07 6:45 
GeneralRe: Leftclick simulation Pin
kim.david.hauser29-Nov-07 5:24
kim.david.hauser29-Nov-07 5:24 
GeneralVista Pin
vovikdoc19-Oct-07 1:40
vovikdoc19-Oct-07 1:40 
GeneralRe: Vista Pin
kim.david.hauser23-Oct-07 2:45
kim.david.hauser23-Oct-07 2:45 
GeneralRe: Vista Pin
vovikdoc24-Oct-07 7:24
vovikdoc24-Oct-07 7:24 
AnswerRe: Vista Pin
Derek Bartram29-Jun-08 9:30
Derek Bartram29-Jun-08 9:30 
GeneralRe: Vista Pin
Derek Bartram29-Jun-08 9:30
Derek Bartram29-Jun-08 9:30 
JokeRe: Vista Pin
vovikdoc30-Jun-08 0:47
vovikdoc30-Jun-08 0:47 
GeneralRe: Vista Pin
CyberZeus3-Jun-09 9:05
CyberZeus3-Jun-09 9:05 
GeneralRe: Vista - Could someone please post a new executable? Pin
Frame2529-Sep-09 11:58
Frame2529-Sep-09 11:58 
QuestionMuti-monitor issues? Pin
CarlosMMartins16-Jul-07 22:37
CarlosMMartins16-Jul-07 22:37 
Questionfrom double click to back Pin
Trent2116-Apr-07 15:22
Trent2116-Apr-07 15:22 
AnswerRe: from double click to back Pin
kim.david.hauser19-Apr-07 5:22
kim.david.hauser19-Apr-07 5:22 

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.