@@ -260,6 +260,30 @@ def filter_destroy(evt):
260260 self .close_event ()
261261 root .bind ("<Destroy>" , filter_destroy , "+" )
262262
263+ # Dictionary for adding modifier keys to the key string.
264+ # Bit details originate from
265+ # http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
266+ # BIT_SHIFT = 0x001; BIT_CAPSLOCK = 0x002; BIT_CONTROL = 0x004;
267+ # BIT_LEFT_ALT = 0x008; BIT_NUMLOCK = 0x010; BIT_RIGHT_ALT = 0x080;
268+ # BIT_MB_1 = 0x100; BIT_MB_2 = 0x200; BIT_MB_3 = 0x400;
269+ # In general, the modifier key is excluded from the modifier flag,
270+ # however this is not the case on "darwin", so double check that
271+ # we aren't adding repeat modifier flags to a modifier key.
272+ if sys .platform == 'win32' :
273+ self .MOD_KEYS = [(17 , 'alt' , 'alt' ),
274+ (2 , 'ctrl' , 'control' ),
275+ ]
276+ elif sys .platform == 'darwin' :
277+ self .MOD_KEYS = [(3 , 'super' , 'super' ),
278+ (4 , 'alt' , 'alt' ),
279+ (2 , 'ctrl' , 'control' ),
280+ ]
281+ else :
282+ self .MOD_KEYS = [(6 , 'super' , 'super' ),
283+ (3 , 'alt' , 'alt' ),
284+ (2 , 'ctrl' , 'control' ),
285+ ]
286+
263287 self ._master = master
264288 self ._tkcanvas .focus_set ()
265289
@@ -395,16 +419,18 @@ def button_press_event(self, event, dblclick=False):
395419 # flipy so y=0 is bottom of canvas
396420 y = self .figure .bbox .height - event .y
397421 num = getattr (event , 'num' , None )
422+ modifiers = self ._get_modifiers (event )
398423
399424 if sys .platform == 'darwin' :
400425 # 2 and 3 were reversed on the OSX platform I
401426 # tested under tkagg
402427 if num == 2 : num = 3
403428 elif num == 3 : num = 2
404429
405- FigureCanvasBase .button_press_event (self , x , y , num , dblclick = dblclick , guiEvent = event )
430+ FigureCanvasBase .button_press_event (self , x , y , num , dblclick = dblclick ,
431+ guiEvent = event , modifiers = modifiers )
406432
407- def button_dblclick_event (self ,event ):
433+ def button_dblclick_event (self , event ):
408434 self .button_press_event (event ,dblclick = True )
409435
410436 def button_release_event (self , event ):
@@ -455,37 +481,19 @@ def _get_key(self, event):
455481 else :
456482 key = None
457483
458- # add modifier keys to the key string. Bit details originate from
459- # http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
460- # BIT_SHIFT = 0x001; BIT_CAPSLOCK = 0x002; BIT_CONTROL = 0x004;
461- # BIT_LEFT_ALT = 0x008; BIT_NUMLOCK = 0x010; BIT_RIGHT_ALT = 0x080;
462- # BIT_MB_1 = 0x100; BIT_MB_2 = 0x200; BIT_MB_3 = 0x400;
463- # In general, the modifier key is excluded from the modifier flag,
464- # however this is not the case on "darwin", so double check that
465- # we aren't adding repeat modifier flags to a modifier key.
466- if sys .platform == 'win32' :
467- modifiers = [(17 , 'alt' , 'alt' ),
468- (2 , 'ctrl' , 'control' ),
469- ]
470- elif sys .platform == 'darwin' :
471- modifiers = [(3 , 'super' , 'super' ),
472- (4 , 'alt' , 'alt' ),
473- (2 , 'ctrl' , 'control' ),
474- ]
475- else :
476- modifiers = [(6 , 'super' , 'super' ),
477- (3 , 'alt' , 'alt' ),
478- (2 , 'ctrl' , 'control' ),
479- ]
480-
481484 if key is not None :
482485 # note, shift is not added to the keys as this is already accounted for
483- for bitmask , prefix , key_name in modifiers :
486+ for bitmask , prefix , key_name in self . MOD_KEYS :
484487 if event .state & (1 << bitmask ) and key_name not in key :
485488 key = '{0}+{1}' .format (prefix , key )
486489
487490 return key
488491
492+ def _get_modifiers (self , event ):
493+ modifiers = {prefix for bitmask , prefix , key_name in self .MOD_KEYS
494+ if event .state & (1 << bitmask )}
495+ return modifiers
496+
489497 def key_press (self , event ):
490498 key = self ._get_key (event )
491499 FigureCanvasBase .key_press_event (self , key , guiEvent = event )
0 commit comments