MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / test_correct_key

Function test_correct_key

lib/matplotlib/tests/test_backend_qt.py:99–131  ·  view source on GitHub ↗

Make a figure. Send a key_press_event event (using non-public, qtX backend specific api). Catch the event. Assert sent and caught keys are the same.

(backend, qt_key, qt_mods, answer, monkeypatch)

Source from the content-addressed store, hash-verified

97 marks=pytest.mark.backend('QtAgg', skip_on_importerror=True)),
98])
99def test_correct_key(backend, qt_key, qt_mods, answer, monkeypatch):
100 """
101 Make a figure.
102 Send a key_press_event event (using non-public, qtX backend specific api).
103 Catch the event.
104 Assert sent and caught keys are the same.
105 """
106 from matplotlib.backends.qt_compat import _to_int, QtCore
107
108 if sys.platform == "darwin" and answer is not None:
109 answer = answer.replace("ctrl", "cmd")
110 answer = answer.replace("control", "cmd")
111 answer = answer.replace("meta", "ctrl")
112 result = None
113 qt_mod = QtCore.Qt.KeyboardModifier.NoModifier
114 for mod in qt_mods:
115 qt_mod |= getattr(QtCore.Qt.KeyboardModifier, mod)
116
117 class _Event:
118 def isAutoRepeat(self): return False
119 def key(self): return _to_int(getattr(QtCore.Qt.Key, qt_key))
120
121 monkeypatch.setattr(QtWidgets.QApplication, "keyboardModifiers",
122 lambda self: qt_mod)
123
124 def on_key_press(event):
125 nonlocal result
126 result = event.key
127
128 qt_canvas = plt.figure().canvas
129 qt_canvas.mpl_connect('key_press_event', on_key_press)
130 qt_canvas.keyPressEvent(_Event())
131 assert result == answer
132
133
134@pytest.mark.backend('QtAgg', skip_on_importerror=True)

Callers

nothing calls this directly

Calls 4

_EventClass · 0.85
figureMethod · 0.80
mpl_connectMethod · 0.80
keyPressEventMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…