Distinguish between single and double click with m

2019-07-21 02:25发布

问题:

I'm trying to catch single clicks and double clicks on my figure.

As stated in another answer, the event contains event.dblclick which is False or True, at least in Version 1.4.2, so double clicks can be got.

The only problem is that it's not easy to distinguish between a single click and a double click because when double-clicking the event gets fired twice. The first time it's with event.dblclick=False and the second time it's with event.dblclick=True.

Is there any solution to this? I saw that the same problem regarding qt is discussed here.

回答1:

You need a software debouncer.

Basically, you start a timer on the 1st click. If the timer runs out, then you proceed to process the single-click event. If a 2nd click is detected within the timer, process a double-click event.

This can actually be expanded to n-clicks if needed. I've found some uses for triple-click events.

Here is one implemented in wxPython. Should be relatively easy to port to matplotlib.


Also, if you're on Windows, I would recommend using the user's double-click speed for your timer (Control Panel: Mouse). You can access it with:

get_double_click_time():
    """ Gets the Windows double click time in ms """
    from ctypes import windll
    return int(windll.user32.GetDoubleClickTime())

I haven't yet figured out how to grab the dclick time from Mac or Linux (but I also don't have a need to).