Click here to Skip to main content
Click here to Skip to main content

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

, 9 Jul 2007
Rate this:
Please Sign up or sign in to vote.
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.

//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:

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

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

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)

About the Author

kim.david.hauser
Software Developer (Senior)
Switzerland Switzerland
programmer and software junkie since 1991 zurich switzerland
Follow on   Twitter   Google+

Comments and Discussions

 
Question3333 Pinmemberits.rajeev237-Mar-13 21:32 
QuestionPlease make a conceptual explanation Pinmemberfvalerin14-Feb-12 6:15 
QuestionLeft Click instead of Middle Click? Pinmemberwizi8312-Jan-10 13:39 
GeneralMbtn2dblclick Question about registry default Pinmemberclargar15-Dec-09 8:40 
GeneralNeeds fixing for southpaws Pinmemberdhstraayer7-Dec-09 8:15 
GeneralWindows 7 - not working in explorer [modified] Pinmemberowen.ward1-Nov-09 7:11 
QuestionNo reaction under XP 32, SP III either PinmemberJ.W. Roberts3-Jul-09 6:33 
GeneralThanks - and a question PinmemberDave Ferrise12-Jan-09 13:04 
GeneralRe: Thanks - and a question PinmemberDave Ferrise12-Jan-09 13:14 
General1 pixel shift Pinmembergenh9-Dec-08 5:12 
QuestionOther keys?? Pinmemberarekkusu8223-Apr-08 14:14 
Is there anyway you could convert the middleclick into something else, like SCROLL LOCK??
GeneralVista x86 PinmemberCyberZeus11-Feb-08 1:10 
GeneralLeftclick simulation Pinmemberfuelairmix27-Nov-07 6:45 
GeneralRe: Leftclick simulation Pinmemberkim.david.hauser29-Nov-07 5:24 
GeneralVista Pinmembervovikdoc19-Oct-07 1:40 
GeneralRe: Vista Pinmemberkim.david.hauser23-Oct-07 2:45 
GeneralRe: Vista Pinmembervovikdoc24-Oct-07 7:24 
AnswerRe: Vista PinmemberDerek Bartram29-Jun-08 9:30 
GeneralRe: Vista PinmemberDerek Bartram29-Jun-08 9:30 
JokeRe: Vista Pinmembervovikdoc30-Jun-08 0:47 
GeneralRe: Vista PinmemberCyberZeus3-Jun-09 9:05 
GeneralRe: Vista - Could someone please post a new executable? PinmemberFrame2529-Sep-09 11:58 
QuestionMuti-monitor issues? PinmemberCarlosMMartins16-Jul-07 22:37 
Questionfrom double click to back PinmemberTrent2116-Apr-07 15:22 
AnswerRe: from double click to back Pinmemberkim.david.hauser19-Apr-07 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by kim.david.hauser
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid