Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #24250 on branch v3.6.x (Fix key reporting in pick events) #24255

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2443,10 +2443,6 @@ def __init__(self,
# pickling.
self._canvas_callbacks = cbook.CallbackRegistry(
signals=FigureCanvasBase.events)
self._button_pick_id = self._canvas_callbacks._connect_picklable(
'button_press_event', self.pick)
self._scroll_pick_id = self._canvas_callbacks._connect_picklable(
'scroll_event', self.pick)
connect = self._canvas_callbacks._connect_picklable
self._mouse_key_ids = [
connect('key_press_event', backend_bases._key_handler),
Expand All @@ -2457,6 +2453,8 @@ def __init__(self,
connect('scroll_event', backend_bases._mouse_handler),
connect('motion_notify_event', backend_bases._mouse_handler),
]
self._button_pick_id = connect('button_press_event', self.pick)
self._scroll_pick_id = connect('scroll_event', self.pick)

if figsize is None:
figsize = mpl.rcParams['figure.figsize']
Expand Down
18 changes: 12 additions & 6 deletions lib/matplotlib/tests/test_backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from matplotlib import path, transforms
from matplotlib.backend_bases import (
FigureCanvasBase, LocationEvent, MouseButton, MouseEvent,
FigureCanvasBase, KeyEvent, LocationEvent, MouseButton, MouseEvent,
NavigationToolbar2, RendererBase)
from matplotlib.backend_tools import RubberbandBase
from matplotlib.figure import Figure
Expand Down Expand Up @@ -124,12 +124,18 @@ def test_pick():
fig = plt.figure()
fig.text(.5, .5, "hello", ha="center", va="center", picker=True)
fig.canvas.draw()

picks = []
fig.canvas.mpl_connect("pick_event", lambda event: picks.append(event))
start_event = MouseEvent(
"button_press_event", fig.canvas, *fig.transFigure.transform((.5, .5)),
MouseButton.LEFT)
fig.canvas.callbacks.process(start_event.name, start_event)
def handle_pick(event):
assert event.mouseevent.key == "a"
picks.append(event)
fig.canvas.mpl_connect("pick_event", handle_pick)

KeyEvent("key_press_event", fig.canvas, "a")._process()
MouseEvent("button_press_event", fig.canvas,
*fig.transFigure.transform((.5, .5)),
MouseButton.LEFT)._process()
KeyEvent("key_release_event", fig.canvas, "a")._process()
assert len(picks) == 1


Expand Down