Click here to Skip to main content
11,802,442 members (66,458 online)
Click here to Skip to main content

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

, 9 Jul 2007 CPOL 79.8K 1.1K 34
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

You may also be interested in...

Comments and Discussions

Question3333 Pin
its.rajeev237-Mar-13 21:32
memberits.rajeev237-Mar-13 21:32 
QuestionPlease make a conceptual explanation Pin
fvalerin14-Feb-12 6:15
memberfvalerin14-Feb-12 6:15 
QuestionLeft Click instead of Middle Click? Pin
wizi8312-Jan-10 13:39
memberwizi8312-Jan-10 13:39 
GeneralMbtn2dblclick Question about registry default Pin
clargar15-Dec-09 8:40
memberclargar15-Dec-09 8:40 
GeneralNeeds fixing for southpaws Pin
dhstraayer7-Dec-09 8:15
memberdhstraayer7-Dec-09 8:15 
GeneralWindows 7 - not working in explorer [modified] Pin
owen.ward1-Nov-09 7:11
memberowen.ward1-Nov-09 7:11 
I downloaded the latest version. In Windows 7, the middle button double-click works on window titles (maximize/restore), but not on folders or files in Windows explorer. It acts like a single click for those. Any plans on getting this to work for Windows 7?

Nevermind. I found another free program that works under Windows 7:[^]

modified on Sunday, November 1, 2009 2:40 PM

QuestionNo reaction under XP 32, SP III either Pin
J.W. Roberts3-Jul-09 6:33
memberJ.W. Roberts3-Jul-09 6:33 
GeneralThanks - and a question Pin
Dave Ferrise12-Jan-09 13:04
memberDave Ferrise12-Jan-09 13:04 
GeneralRe: Thanks - and a question Pin
Dave Ferrise12-Jan-09 13:14
memberDave Ferrise12-Jan-09 13:14 
General1 pixel shift Pin
genh9-Dec-08 5:12
membergenh9-Dec-08 5:12 
QuestionOther keys?? Pin
arekkusu8223-Apr-08 14:14
memberarekkusu8223-Apr-08 14:14 
GeneralVista x86 Pin
CyberZeus11-Feb-08 1:10
memberCyberZeus11-Feb-08 1:10 
GeneralLeftclick simulation Pin
fuelairmix27-Nov-07 6:45
memberfuelairmix27-Nov-07 6:45 
GeneralRe: Leftclick simulation Pin
kim.david.hauser29-Nov-07 5:24
memberkim.david.hauser29-Nov-07 5:24 
GeneralVista Pin
vovikdoc19-Oct-07 1:40
membervovikdoc19-Oct-07 1:40 
GeneralRe: Vista Pin
kim.david.hauser23-Oct-07 2:45
memberkim.david.hauser23-Oct-07 2:45 
GeneralRe: Vista Pin
vovikdoc24-Oct-07 7:24
membervovikdoc24-Oct-07 7:24 
AnswerRe: Vista Pin
Derek Bartram29-Jun-08 9:30
memberDerek Bartram29-Jun-08 9:30 
GeneralRe: Vista Pin
Derek Bartram29-Jun-08 9:30
memberDerek Bartram29-Jun-08 9:30 
JokeRe: Vista Pin
vovikdoc30-Jun-08 0:47
membervovikdoc30-Jun-08 0:47 
GeneralRe: Vista Pin
CyberZeus3-Jun-09 9:05
memberCyberZeus3-Jun-09 9:05 
GeneralRe: Vista - Could someone please post a new executable? Pin
Frame2529-Sep-09 11:58
memberFrame2529-Sep-09 11:58 
QuestionMuti-monitor issues? Pin
CarlosMMartins16-Jul-07 22:37
memberCarlosMMartins16-Jul-07 22:37 
Questionfrom double click to back Pin
Trent2116-Apr-07 15:22
memberTrent2116-Apr-07 15:22 
AnswerRe: from double click to back Pin
kim.david.hauser19-Apr-07 5:22
memberkim.david.hauser19-Apr-07 5:22 
GeneralMake Icon Go Away Pin
rethaew13-Apr-07 17:38
memberrethaew13-Apr-07 17:38 
GeneralRe: Make Icon Go Away Pin
madulad20-Apr-07 1:57
membermadulad20-Apr-07 1:57 
QuestionCan it be modified for the Explorer only ? Pin
Jean-François23-Feb-07 9:33
memberJean-François23-Feb-07 9:33 
AnswerRe: Can it be modified for the Explorer only ? [modified] Pin
kim.david.hauser24-Feb-07 5:53
memberkim.david.hauser24-Feb-07 5:53 
AnswerRe: Can it be modified for the Explorer only ? Pin
Jean-François24-Feb-07 10:01
memberJean-François24-Feb-07 10:01 
GeneralProblem with "Download source code" Pin
Jean-François22-Feb-07 5:53
memberJean-François22-Feb-07 5:53 
GeneralRe: Problem with "Download source code" Pin
kim.david.hauser23-Feb-07 5:07
memberkim.david.hauser23-Feb-07 5:07 

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
Web03 | 2.8.151002.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by kim.david.hauser
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid