Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a faulty mouse that sometimes double clicks when I only single click, left click, I had the same trouble with a keyboard with the space key and I made it suppress the 2nd space key keydown by e.Handled=true, how to do it for global mouse clicks?

I just need the way to globally detect mouseclicks with the ability to suppress clicks and not have them clicking twice when I clicked only once sometimes.

What I have tried:

I want something like this but for mouse clicks globally:
public static globalKeyboardHook gkh = new globalKeyboardHook();



static DateTime lastTimeSpaceKeyUp = DateTime.Now;
public static void Gkh_KeyUp(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Space:
            if ((DateTime.Now - lastTimeSpaceKeyUp).TotalMilliseconds < 20)
            {
                e.Handled = true;
                //MessageBox.Show(((DateTime.Now - lastTimeSpaceKeyUp).TotalMilliseconds).ToString());
            }
            break;
    }
    lastTimeSpaceKeyUp = DateTime.Now;
}







static DateTime lastTimeSpaceKeyDown = DateTime.Now;
public static void Gkh_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Space:
            if ((DateTime.Now - lastTimeSpaceKeyDown).TotalMilliseconds < 20)
            {
                e.Handled = true;
                //MessageBox.Show(((DateTime.Now - lastTimeSpaceKeyDown).TotalMilliseconds).ToString());
            }
            break;
    }
    lastTimeSpaceKeyDown = DateTime.Now;
}










I made this:
DateTime timeLastLeftClickDown = DateTime.Now;
DateTime timeLastLeftClickUp = DateTime.Now;
public int LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0)
    {
        // Get the mouse WM from the wParam parameter
        var wmMouse = (MouseMessage)wParam;
        if (wmMouse == MouseMessage.WM_LBUTTONDOWN || wmMouse == MouseMessage.WM_LBUTTONDBLCLK || wmMouse == MouseMessage.WM_NCLBUTTONDOWN) //&& LeftButtonState == ButtonState.Released)
        {
            int d = (int)(DateTime.Now - timeLastLeftClickDown).TotalMilliseconds;
            timeLastLeftClickDown = DateTime.Now;

            if (d < 108)
            {
                //MessageBox.Show(d.ToString());
                return -1;
                //return NativeMethods.CallNextHookEx(_hGlobalLlMouseHook, /*nCode*/  -1, wParam, lParam);
            }

            //Logger.Debug("Left Mouse down");
        }
        else
        if (wmMouse == MouseMessage.WM_LBUTTONUP || wmMouse == MouseMessage.WM_NCLBUTTONUP) //&& LeftButtonState == ButtonState.Released)
        {
            int d = (int)(DateTime.Now - timeLastLeftClickUp).TotalMilliseconds;
            timeLastLeftClickUp = DateTime.Now;

            if (d < 108)
            {
                //MessageBox.Show(d.ToString());

                return -1;
                //return NativeMethods.CallNextHookEx(_hGlobalLlMouseHook, /*nCode*/  -1, wParam, lParam);
            }
            //Logger.Debug("Left Mouse up");
        }

        //if (wmMouse == MouseMessage.WM_RBUTTONDOWN)//&& RightButtonState == ButtonState.Released)
        //{
        //    //Logger.Debug("Right Mouse down");
        //}
        //if (wmMouse == MouseMessage.WM_RBUTTONUP)//&& RightButtonState == ButtonState.Down)
        //{
        //    //Logger.Debug("Right Mouse up");
        //}
    }

    // Pass the hook information to the next hook procedure in chain
    return NativeMethods.CallNextHookEx(_hGlobalLlMouseHook, nCode, wParam, lParam);
}
Posted
Updated 8-May-23 9:55am
v15
Comments
0x01AA 8-May-23 13:07pm    
'I have a faulty mouse that sometimes double clicks when I only single click': How is about to buy a new mouse?
RobertoSky 8-May-23 13:11pm    
I still want to deal with it please.
Dave Kreskowiak 8-May-23 18:40pm    
So, uhhhh... How are you doing to determine what is a fake double-click compared to a real double-click??

1 solution

See StackOverflow:
is-there-any-way-to-global-hook-the-mouse-actions-like-im-hooking-the-keyboard[^]

A simpler option for a WinForms application might be to use GlobalEventProvider, see:
how-to-make-an-application-that-can-capture-all-click-when-you-press-the-enter-k[^]
 
Share this answer
 
v2
Comments
RobertoSky 8-May-23 14:27pm    
Check the new code please.
RickZeeland 8-May-23 14:57pm    
I have not used this code myself, but it seems to me that you only want to pass the hook information to the next hook procedure in chain when it is a valid mouse click. You need to program some logic for this.
RobertoSky 8-May-23 15:33pm    
What should I pass here:
return NativeMethods.CallNextHookEx(_hGlobalLlMouseHook, nCode, wParam, lParam);
If I want to suppress the mouse click?
RickZeeland 8-May-23 15:36pm    
Skip that if it is not a valid mouse click, then it will not be processed.
Before modifying the example code, use the debugger to see what the return codes of this method look like.
RobertoSky 8-May-23 15:39pm    
But I have to return something in:
int LowLevelMouseProc()
what to return plz?
Edit: I made return -1;
seems to work well right?
Edit2: I tested it with making the time 2000 ms to prevent next clicks, and the next clicks were suppressed for 2 seconds since the last one, I think it works correctly.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900