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 CPOL
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

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


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


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() {
    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 ) {
    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
    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

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


  • 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


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


About the Author

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 22:32 
QuestionPlease make a conceptual explanation Pinmemberfvalerin14-Feb-12 7:15 
QuestionLeft Click instead of Middle Click? Pinmemberwizi8312-Jan-10 14:39 
GeneralMbtn2dblclick Question about registry default Pinmemberclargar15-Dec-09 9:40 
GeneralNeeds fixing for southpaws Pinmemberdhstraayer7-Dec-09 9:15 
GeneralWindows 7 - not working in explorer [modified] Pinmemberowen.ward1-Nov-09 8:11 
QuestionNo reaction under XP 32, SP III either PinmemberJ.W. Roberts3-Jul-09 7:33 
Hi Kim,
Thanx for the Utility which I found while searching for an explanation why my wheel click using the venerable "ToggleMouse" stopped working. This happened after "moving" my PC to a new machine and updating to XP (32) SP III and Internet Explorer 8.
I installed your executable just fine but there's no reaction when clicking the wheel at all. Now, as this is not the only device that stopped working the pen on my graphics tablet (HDI) went dead as well, I guess that MS is bending/redirecting some interrupts Mad | :mad:
Do you or does anybody happen to know of a tool whereby I might diagnose the problem more specifically, or even remedy it???
I would be very grateful, thank you or any respondent/s in advance.
Kind regards,
Jan Willem Roberts
PS I'm not a programmer ( anymore - aged 66 now :-{)= )
GeneralThanks - and a question PinmemberDave Ferrise12-Jan-09 14:04 
GeneralRe: Thanks - and a question PinmemberDave Ferrise12-Jan-09 14:14 
General1 pixel shift Pinmembergenh9-Dec-08 6:12 
QuestionOther keys?? Pinmemberarekkusu8223-Apr-08 15:14 
GeneralVista x86 PinmemberCyberZeus11-Feb-08 2:10 
GeneralLeftclick simulation Pinmemberfuelairmix27-Nov-07 7:45 
GeneralRe: Leftclick simulation Pinmemberkim.david.hauser29-Nov-07 6:24 
GeneralVista Pinmembervovikdoc19-Oct-07 2:40 
GeneralRe: Vista Pinmemberkim.david.hauser23-Oct-07 3:45 
GeneralRe: Vista Pinmembervovikdoc24-Oct-07 8:24 
AnswerRe: Vista PinmemberDerek Bartram29-Jun-08 10:30 
GeneralRe: Vista PinmemberDerek Bartram29-Jun-08 10:30 
JokeRe: Vista Pinmembervovikdoc30-Jun-08 1:47 
GeneralRe: Vista PinmemberCyberZeus3-Jun-09 10:05 
GeneralRe: Vista - Could someone please post a new executable? PinmemberFrame2529-Sep-09 12:58 
QuestionMuti-monitor issues? PinmemberCarlosMMartins16-Jul-07 23:37 
Questionfrom double click to back PinmemberTrent2116-Apr-07 16:22 
AnswerRe: from double click to back Pinmemberkim.david.hauser19-Apr-07 6: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 | Terms of Use | Mobile
Web01 | 2.8.150224.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by kim.david.hauser
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid