Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LowLevelMouseHook (double-click & timing) #36

Open
w4po opened this issue Feb 4, 2025 · 0 comments
Open

LowLevelMouseHook (double-click & timing) #36

w4po opened this issue Feb 4, 2025 · 0 comments

Comments

@w4po
Copy link

w4po commented Feb 4, 2025

Hello,
I needed a fast way to detect a mouse double-click, I used the DoubleClick event but I found it slow for some reason,

After a quick look, I figured out why, it never catches the WM_LBUTTONDBLCLK message (I've implemented my mouse low-level hook and the WM_LBUTTONDBLCLK message isn't fired at all).

So in the LowLevelMouseHook class the isDoubleClick never true,
and the DoubleClick event is fired on the mouseUp portion:

if (mouseUp)
{
    OnUp(args);
    PreviousDownTimeDictionary.TryGetValue(key, out var previousDateTime);
    if (DateTime.Now.Subtract(previousDateTime) < DoubleClickSpeed)
    {
        OnDoubleClick(args);
    }
}

Which is not that fast because at the second click the user might be slow to release it.

So I think it would be better to check for double-click on the mouseDown portion (Which isn't always what everyone needs)
So an option to choose would be much better.

The other thing that I noticed is the current implementation checks for timing using DateTime.Now, I think it would be better to use Environment.TickCount

Here is how I solved the issue:

    private static void MouseHookOnDown(object? _, MouseEventArgs e)
    {
        if (e.CurrentKey != Key.MouseLeft) return;

        var now = Environment.TickCount;
        if (now - _lastClickTime < 400)
        {
            // Double-click detected
        }
        _lastClickTime = now;
    }

Thank you for your awesome work <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant