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;
}
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;
}
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)
{
var wmMouse = (MouseMessage)wParam;
if (wmMouse == MouseMessage.WM_LBUTTONDOWN || wmMouse == MouseMessage.WM_LBUTTONDBLCLK || wmMouse == MouseMessage.WM_NCLBUTTONDOWN)
{
int d = (int)(DateTime.Now - timeLastLeftClickDown).TotalMilliseconds;
timeLastLeftClickDown = DateTime.Now;
if (d < 108)
{
return -1;
}
}
else
if (wmMouse == MouseMessage.WM_LBUTTONUP || wmMouse == MouseMessage.WM_NCLBUTTONUP)
{
int d = (int)(DateTime.Now - timeLastLeftClickUp).TotalMilliseconds;
timeLastLeftClickUp = DateTime.Now;
if (d < 108)
{
return -1;
}
}
}
return NativeMethods.CallNextHookEx(_hGlobalLlMouseHook, nCode, wParam, lParam);
}