Click here to Skip to main content
Licence CPOL
First Posted 14 Feb 2007
Views 62,429
Downloads 730
Bookmarked 33 times

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

By | 9 Jul 2007 | Article
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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionPlease make a conceptual explanation Pinmemberfvalerin6:15 14 Feb '12  
QuestionLeft Click instead of Middle Click? Pinmemberwizi8313:39 12 Jan '10  
GeneralMbtn2dblclick Question about registry default Pinmemberclargar8:40 15 Dec '09  
GeneralNeeds fixing for southpaws Pinmemberdhstraayer8:15 7 Dec '09  
GeneralWindows 7 - not working in explorer [modified] Pinmemberowen.ward7:11 1 Nov '09  
QuestionNo reaction under XP 32, SP III either PinmemberJ.W. Roberts6:33 3 Jul '09  
GeneralThanks - and a question PinmemberDave Ferrise13:04 12 Jan '09  
GeneralRe: Thanks - and a question PinmemberDave Ferrise13:14 12 Jan '09  
General1 pixel shift Pinmembergenh5:12 9 Dec '08  
QuestionOther keys?? Pinmemberarekkusu8214:14 23 Apr '08  
GeneralVista x86 PinmemberCyberZeus1:10 11 Feb '08  
Thank you Kim for your code!!!
I have a Logitech mouse (Optical Cordless) that SetPoint by Logitech doesn't support. Because of MouseWare cannot be installed on Vista x86, I was searching for an app or tool to simulate double-click from middle-button-clicks.
I am very happy about your application. Big Grin | :-D
I have also a cordless keyboard by Logitech, and this keyboard doesn't have leds for Caps Lock, Num Lock and Scroll Lock. Logitech app makes a box appear that says you the state of the Locks, but not in Vista! Frown | :( So I will try to make an app that hooks a Lock press and display a box of its state.
See you here when I'll be done.
 
Cheers,
CyberZeus
GeneralLeftclick simulation Pinmemberfuelairmix6:45 27 Nov '07  
GeneralRe: Leftclick simulation Pinmemberkim.david.hauser5:24 29 Nov '07  
GeneralVista Pinmembervovikdoc1:40 19 Oct '07  
GeneralRe: Vista Pinmemberkim.david.hauser2:45 23 Oct '07  
GeneralRe: Vista Pinmembervovikdoc7:24 24 Oct '07  
AnswerRe: Vista PinmemberDerek Bartram9:30 29 Jun '08  
GeneralRe: Vista PinmemberDerek Bartram9:30 29 Jun '08  
JokeRe: Vista Pinmembervovikdoc0:47 30 Jun '08  
GeneralRe: Vista PinmemberCyberZeus9:05 3 Jun '09  
GeneralRe: Vista - Could someone please post a new executable? PinmemberFrame2511:58 29 Sep '09  
QuestionMuti-monitor issues? PinmemberCarlosMMartins22:37 16 Jul '07  
Questionfrom double click to back PinmemberTrent2115:22 16 Apr '07  
AnswerRe: from double click to back Pinmemberkim.david.hauser5:22 19 Apr '07  
GeneralMake Icon Go Away Pinmemberrethaew17:38 13 Apr '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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by kim.david.hauser
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid