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

Skip to content

Commit 3bbbd0a

Browse files
Phil Elsonpelson
Phil Elson
authored andcommitted
added modifier key support to the tk backend (i.e. ctrl+key). Additional support will need to be added for qt, gtk and wx backends.
1 parent dc77096 commit 3bbbd0a

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ class KeyEvent(LocationEvent):
14001400
attributes, the following attributes are defined:
14011401
14021402
*key*
1403-
the key pressed: None, chr(range(255), shift, win, or control
1403+
the key pressed: None, chr(range(255)), shift, win, or control
14041404
14051405
This interface may change slightly when better support for
14061406
modifier keys is included.
@@ -2296,10 +2296,10 @@ def key_press_handler(event, canvas, toolbar=None):
22962296
if event.key in fullscreen_keys:
22972297
canvas.manager.full_screen_toggle()
22982298

2299-
# quit the figure (defaut key 'q')
2299+
# quit the figure (defaut key 'ctrl+w')
23002300
if event.key in quit_keys:
23012301
Gcf.destroy_fig(canvas.figure)
2302-
2302+
23032303
if toolbar is not None:
23042304
# home or reset mnemonic (default key 'h', 'home' and 'r')
23052305
if event.key in home_keys:

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,24 @@ def _get_key(self, event):
399399
val = event.keysym_num
400400
if val in self.keyvald:
401401
key = self.keyvald[val]
402-
elif val<256:
402+
elif val < 256:
403403
key = chr(val)
404404
else:
405405
key = None
406+
407+
# add modifier keys to the key string. Bit details originate from
408+
# http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
409+
# BIT_SHIFT = 0x001; BIT_CAPSLOCK = 0x002; BIT_CONTROL = 0x004;
410+
# BIT_LEFT_ALT = 0x008; BIT_NUMLOCK = 0x010; BIT_RIGHT_ALT = 0x080;
411+
# BIT_MB_1 = 0x100; BIT_MB_2 = 0x200; BIT_MB_3 = 0x400;
412+
if key is not None:
413+
# note, shift is not added to the keys as this is already accounted for
414+
for bitmask, prefix in [(2, 'ctrl'), (3, 'alt')]:
415+
if event.state & (1 << bitmask):
416+
key = '{}+{}'.format(prefix, key)
417+
406418
return key
407419

408-
409420
def key_press(self, event):
410421
key = self._get_key(event)
411422
FigureCanvasBase.key_press_event(self, key, guiEvent=event)

lib/matplotlib/rcsetup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,14 +575,14 @@ def __call__(self, s):
575575
# recommend about 20000 to
576576
# enable. Experimental.
577577
# key-mappings
578-
'keymap.fullscreen' : ['f', validate_stringlist],
578+
'keymap.fullscreen' : [('f', 'ctrl+f'), validate_stringlist],
579579
'keymap.home' : [['h', 'r', 'home'], validate_stringlist],
580580
'keymap.back' : [['left', 'c', 'backspace'], validate_stringlist],
581581
'keymap.forward' : [['right', 'v'], validate_stringlist],
582582
'keymap.pan' : ['p', validate_stringlist],
583583
'keymap.zoom' : ['o', validate_stringlist],
584-
'keymap.save' : ['s', validate_stringlist],
585-
'keymap.quit' : ['q', validate_stringlist],
584+
'keymap.save' : [('s', 'ctrl+s'), validate_stringlist],
585+
'keymap.quit' : [('ctrl+w', 'escape'), validate_stringlist],
586586
'keymap.grid' : ['g', validate_stringlist],
587587
'keymap.yscale' : ['l', validate_stringlist],
588588
'keymap.xscale' : [['k', 'L'], validate_stringlist],

matplotlibrc.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
410410
#keymap.pan : p # pan mnemonic
411411
#keymap.zoom : o # zoom mnemonic
412412
#keymap.save : s # saving current figure
413-
#keymap.quit : q # close the current figure
413+
#keymap.quit : ctrl+w # close the current figure
414414
#keymap.grid : g # switching on/off a grid in current axes
415415
#keymap.yscale : l # toggle scaling of y-axes ('log'/'linear')
416416
#keymap.xscale : L, k # toggle scaling of x-axes ('log'/'linear')

0 commit comments

Comments
 (0)