Skip to content

Commit 2ce4404

Browse files
authored
Merge pull request #18693 from anntzer/tkkeys
Also fix tk key mapping, following the same strategy as for gtk.
2 parents 4b244a7 + 57b9c26 commit 2ce4404

File tree

4 files changed

+33
-89
lines changed

4 files changed

+33
-89
lines changed

doc/api/next_api_changes/behavior/17791-AL.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
GTK key name changes
2-
~~~~~~~~~~~~~~~~~~~~
1+
GTK/Tk key name changes
2+
~~~~~~~~~~~~~~~~~~~~~~~
33

44
The handling of non-ASCII keypresses (as reported in the KeyEvent passed to
5-
``key_press_event``-handlers) in the GTK backends now correctly reports Unicode
6-
characters (e.g., €), and respects NumLock on the numpad.
5+
``key_press_event``-handlers) in the GTK and Tk backends now correctly reports
6+
Unicode characters (e.g., €), and better respects NumLock on the numpad.
77

88
The following key names have changed; the new names are consistent with those
99
reported by the Qt backends:

lib/matplotlib/backends/_backend_tk.py

+1-74
Original file line numberDiff line numberDiff line change
@@ -113,70 +113,6 @@ def _on_timer(self):
113113
class FigureCanvasTk(FigureCanvasBase):
114114
required_interactive_framework = "tk"
115115

116-
keyvald = {65507: 'control',
117-
65505: 'shift',
118-
65513: 'alt',
119-
65515: 'super',
120-
65508: 'control',
121-
65506: 'shift',
122-
65514: 'alt',
123-
65361: 'left',
124-
65362: 'up',
125-
65363: 'right',
126-
65364: 'down',
127-
65307: 'escape',
128-
65470: 'f1',
129-
65471: 'f2',
130-
65472: 'f3',
131-
65473: 'f4',
132-
65474: 'f5',
133-
65475: 'f6',
134-
65476: 'f7',
135-
65477: 'f8',
136-
65478: 'f9',
137-
65479: 'f10',
138-
65480: 'f11',
139-
65481: 'f12',
140-
65300: 'scroll_lock',
141-
65299: 'break',
142-
65288: 'backspace',
143-
65293: 'enter',
144-
65379: 'insert',
145-
65535: 'delete',
146-
65360: 'home',
147-
65367: 'end',
148-
65365: 'pageup',
149-
65366: 'pagedown',
150-
65438: '0',
151-
65436: '1',
152-
65433: '2',
153-
65435: '3',
154-
65430: '4',
155-
65437: '5',
156-
65432: '6',
157-
65429: '7',
158-
65431: '8',
159-
65434: '9',
160-
65451: '+',
161-
65453: '-',
162-
65450: '*',
163-
65455: '/',
164-
65439: 'dec',
165-
65421: 'enter',
166-
}
167-
168-
_keycode_lookup = {
169-
262145: 'control',
170-
524320: 'alt',
171-
524352: 'alt',
172-
1048584: 'super',
173-
1048592: 'super',
174-
131074: 'shift',
175-
131076: 'shift',
176-
}
177-
"""_keycode_lookup is used for badly mapped (i.e. no event.key_sym set)
178-
keys on apple keyboards."""
179-
180116
def __init__(self, figure, master=None, resize_callback=None):
181117
super().__init__(figure)
182118
self._idle = True
@@ -332,16 +268,7 @@ def scroll_event_windows(self, event):
332268
FigureCanvasBase.scroll_event(self, x, y, step, guiEvent=event)
333269

334270
def _get_key(self, event):
335-
val = event.keysym_num
336-
if val in self.keyvald:
337-
key = self.keyvald[val]
338-
elif (val == 0 and sys.platform == 'darwin'
339-
and event.keycode in self._keycode_lookup):
340-
key = self._keycode_lookup[event.keycode]
341-
elif val < 256:
342-
key = chr(val)
343-
else:
344-
key = None
271+
key = cbook._unikey_or_keysym_to_mplkey(event.char, event.keysym)
345272

346273
# add modifier keys to the key string. Bit details originate from
347274
# http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

lib/matplotlib/backends/backend_gtk3.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,9 @@ def size_allocate(self, widget, allocation):
207207
self.draw_idle()
208208

209209
def _get_key(self, event):
210-
key = chr(Gdk.keyval_to_unicode(event.keyval))
211-
if not key.isprintable():
212-
key = Gdk.keyval_name(event.keyval).lower()
213-
if key.startswith("kp_"): # keypad_x (including kp_enter).
214-
key = key[3:]
215-
if key.startswith("page_"): # page_{up,down}
216-
key = key.replace("page_", "page")
217-
if key.endswith(("_l", "_r")): # alt_l, ctrl_l, shift_l.
218-
key = key[:-2]
219-
if key == "enter":
220-
key = "return"
210+
key = cbook._unikey_or_keysym_to_mplkey(
211+
chr(Gdk.keyval_to_unicode(event.keyval)),
212+
Gdk.keyval_name(event.keyval))
221213
modifiers = [
222214
(Gdk.ModifierType.MOD4_MASK, 'super'),
223215
(Gdk.ModifierType.MOD1_MASK, 'alt'),

lib/matplotlib/cbook/__init__.py

+25
Original file line numberDiff line numberDiff line change
@@ -2334,3 +2334,28 @@ def _format_approx(number, precision):
23342334
Remove trailing zeros and possibly the decimal point.
23352335
"""
23362336
return f'{number:.{precision}f}'.rstrip('0').rstrip('.') or '0'
2337+
2338+
2339+
def _unikey_or_keysym_to_mplkey(unikey, keysym):
2340+
"""
2341+
Convert a Unicode key or X keysym to a Matplotlib key name.
2342+
2343+
The Unicode key is checked first; this avoids having to list most printable
2344+
keysyms such as ``EuroSign``.
2345+
"""
2346+
# For non-printable characters, gtk3 passes "\0" whereas tk passes an "".
2347+
if unikey and unikey.isprintable():
2348+
return unikey
2349+
key = keysym.lower()
2350+
if key.startswith("kp_"): # keypad_x (including kp_enter).
2351+
key = key[3:]
2352+
if key.startswith("page_"): # page_{up,down}
2353+
key = key.replace("page_", "page")
2354+
if key.endswith(("_l", "_r")): # alt_l, ctrl_l, shift_l.
2355+
key = key[:-2]
2356+
key = {
2357+
"enter": "return",
2358+
"prior": "pageup", # Used by tk.
2359+
"next": "pagedown", # Used by tk.
2360+
}.get(key, key)
2361+
return key

0 commit comments

Comments
 (0)