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

mouse.on_double_click not working #3

Closed
dino8890 opened this issue Oct 5, 2017 · 3 comments
Closed

mouse.on_double_click not working #3

dino8890 opened this issue Oct 5, 2017 · 3 comments

Comments

@dino8890
Copy link

dino8890 commented Oct 5, 2017

Hi,

I have the issue where callback passed to mouse.on_double_click isn't invoked.

I took a quick peek and it seems that handler method defined in mouse.on_button gets called twice successively. Each ButtonEvent passed to the handler has button = 'left' and event_type = 'down' members, instead of button = 'left' and event_type = 'double' which the handler checks for.

This is the code I'm talking about.

def on_button(callback, args=(), buttons=(LEFT, MIDDLE, RIGHT, X, X2), types=(UP, DOWN, DOUBLE)):
    """ Invokes `callback` with `args` when the specified event happens. """
    if not isinstance(buttons, (tuple, list)):
        buttons = (buttons,)
    if not isinstance(types, (tuple, list)):
        types = (types,)

    def handler(event):
        if isinstance(event, ButtonEvent):
            if event.event_type in types and event.button in buttons:
                callback(*args)
    _listener.add_handler(handler)
    return handler

I'm a little busy at the moment, but if I find time I'll make an attempt at fixing this.

@boppreh
Copy link
Owner

boppreh commented Oct 5, 2017

It seems Windows is not reporting double click events when a global event hook is registered.

I couldn't find a way to get these events reported, so it probably needs processing the left clicks and checking for interval. Note to self: the maximum interval to be considered a double click is available via the GetDoubleClickTime() win32 function.

Thanks for the report. I'm extremely busy at the moment, but I'll take a look when I can. Feel free to work on it too.

@dino8890
Copy link
Author

dino8890 commented Oct 7, 2017

I modified low_level_mouse_handler callback in _winmouse.py to calculate time difference between previous and current event and create new ButtonEvent of type DOUBLE if the difference is less or equal to the value returned by GetDoubleClickTime.

    GetDoubleClickTime = user32.GetDoubleClickTime

    ...

    previous_button_event = None # defined in global scope

    ...

    def low_level_mouse_handler(nCode, wParam, lParam):
        global previous_button_event

        struct = lParam.contents
        # Can't use struct.time because it's usually zero.
        t = time.time()

        if wParam == WM_MOUSEMOVE:
            event = MoveEvent(struct.x, struct.y, t)
        elif wParam == WM_MOUSEWHEEL:
            event = WheelEvent(struct.data / (WHEEL_DELTA * (2<<15)), t)
        elif wParam in buttons_by_wm_code:
            type, button = buttons_by_wm_code.get(wParam, ('?', '?'))
            if wParam >= WM_XBUTTONDOWN:
                button = {0x10000: X, 0x20000: X2}[struct.data]
            event = ButtonEvent(type, button, t)

            if (event.event_type == DOWN) and previous_button_event is not None:
                diff = int(round(event.time * 1000.0)) - int(round(previous_button_event.time * 1000.0))
                if diff <= GetDoubleClickTime():
                    event = ButtonEvent(DOUBLE, event.button, event.time)

            previous_button_event = event

        queue.put(event)
        return CallNextHookEx(NULL, nCode, wParam, lParam)

So far this works as intented and generates UP, DOWN, DOUBLE, UP event sequence which is a valid double click event sequence according to this MSDN article.

What do you think of this solution?

@boppreh
Copy link
Owner

boppreh commented Oct 7, 2017

Thank you, that works great. It's added by commit bc8d989 .

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

2 participants