|
19 | 19 | from matplotlib import _api, backend_tools, cbook, _c_internal_utils
|
20 | 20 | from matplotlib.backend_bases import (
|
21 | 21 | _Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
|
22 |
| - TimerBase, ToolContainerBase, cursors, _Mode, |
| 22 | + TimerBase, ToolContainerBase, cursors, _Mode, MouseButton, |
23 | 23 | CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent)
|
24 | 24 | from matplotlib._pylab_helpers import Gcf
|
25 | 25 | from . import _tkagg
|
@@ -296,6 +296,7 @@ def _event_mpl_coords(self, event):
|
296 | 296 | def motion_notify_event(self, event):
|
297 | 297 | MouseEvent("motion_notify_event", self,
|
298 | 298 | *self._event_mpl_coords(event),
|
| 299 | + buttons=self._mpl_buttons(event), |
299 | 300 | modifiers=self._mpl_modifiers(event),
|
300 | 301 | guiEvent=event)._process()
|
301 | 302 |
|
@@ -357,13 +358,33 @@ def scroll_event_windows(self, event):
|
357 | 358 | x, y, step=step, modifiers=self._mpl_modifiers(event),
|
358 | 359 | guiEvent=event)._process()
|
359 | 360 |
|
| 361 | + @staticmethod |
| 362 | + def _mpl_buttons(event): # See _mpl_modifiers. |
| 363 | + # NOTE: This fails to report multiclicks on macOS; only one button is |
| 364 | + # reported (multiclicks work correctly on Linux & Windows). |
| 365 | + modifiers = [ |
| 366 | + # macOS appears to swap right and middle (look for "Swap buttons |
| 367 | + # 2/3" in tk/macosx/tkMacOSXMouseEvent.c). |
| 368 | + (MouseButton.LEFT, 1 << 8), |
| 369 | + (MouseButton.RIGHT, 1 << 9), |
| 370 | + (MouseButton.MIDDLE, 1 << 10), |
| 371 | + (MouseButton.BACK, 1 << 11), |
| 372 | + (MouseButton.FORWARD, 1 << 12), |
| 373 | + ] if sys.platform == "darwin" else [ |
| 374 | + (MouseButton.LEFT, 1 << 8), |
| 375 | + (MouseButton.MIDDLE, 1 << 9), |
| 376 | + (MouseButton.RIGHT, 1 << 10), |
| 377 | + (MouseButton.BACK, 1 << 11), |
| 378 | + (MouseButton.FORWARD, 1 << 12), |
| 379 | + ] |
| 380 | + # State *before* press/release. |
| 381 | + return [name for name, mask in modifiers if event.state & mask] |
| 382 | + |
360 | 383 | @staticmethod
|
361 | 384 | def _mpl_modifiers(event, *, exclude=None):
|
362 |
| - # add modifier keys to the key string. Bit details originate from |
363 |
| - # http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm |
364 |
| - # BIT_SHIFT = 0x001; BIT_CAPSLOCK = 0x002; BIT_CONTROL = 0x004; |
365 |
| - # BIT_LEFT_ALT = 0x008; BIT_NUMLOCK = 0x010; BIT_RIGHT_ALT = 0x080; |
366 |
| - # BIT_MB_1 = 0x100; BIT_MB_2 = 0x200; BIT_MB_3 = 0x400; |
| 385 | + # Add modifier keys to the key string. Bit values are inferred from |
| 386 | + # the implementation of tkinter.Event.__repr__ (1, 2, 4, 8, ... = |
| 387 | + # Shift, Lock, Control, Mod1, ..., Mod5, Button1, ..., Button5) |
367 | 388 | # In general, the modifier key is excluded from the modifier flag,
|
368 | 389 | # however this is not the case on "darwin", so double check that
|
369 | 390 | # we aren't adding repeat modifier flags to a modifier key.
|
|
0 commit comments