@@ -209,6 +209,30 @@ def filter_destroy(evt):
209209 self .close_event ()
210210 root .bind ("<Destroy>" , filter_destroy , "+" )
211211
212+ # Dictionary for adding modifier keys to the key string.
213+ # Bit details originate from
214+ # http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
215+ # BIT_SHIFT = 0x001; BIT_CAPSLOCK = 0x002; BIT_CONTROL = 0x004;
216+ # BIT_LEFT_ALT = 0x008; BIT_NUMLOCK = 0x010; BIT_RIGHT_ALT = 0x080;
217+ # BIT_MB_1 = 0x100; BIT_MB_2 = 0x200; BIT_MB_3 = 0x400;
218+ # In general, the modifier key is excluded from the modifier flag,
219+ # however this is not the case on "darwin", so double check that
220+ # we aren't adding repeat modifier flags to a modifier key.
221+ if sys .platform == 'win32' :
222+ self .MOD_KEYS = [(17 , 'alt' , 'alt' ),
223+ (2 , 'ctrl' , 'control' ),
224+ ]
225+ elif sys .platform == 'darwin' :
226+ self .MOD_KEYS = [(3 , 'super' , 'super' ),
227+ (4 , 'alt' , 'alt' ),
228+ (2 , 'ctrl' , 'control' ),
229+ ]
230+ else :
231+ self .MOD_KEYS = [(6 , 'super' , 'super' ),
232+ (3 , 'alt' , 'alt' ),
233+ (2 , 'ctrl' , 'control' ),
234+ ]
235+
212236 self ._master = master
213237 self ._tkcanvas .focus_set ()
214238
@@ -344,16 +368,18 @@ def button_press_event(self, event, dblclick=False):
344368 # flipy so y=0 is bottom of canvas
345369 y = self .figure .bbox .height - event .y
346370 num = getattr (event , 'num' , None )
371+ modifiers = self ._get_modifiers (event )
347372
348373 if sys .platform == 'darwin' :
349374 # 2 and 3 were reversed on the OSX platform I
350375 # tested under tkagg
351376 if num == 2 : num = 3
352377 elif num == 3 : num = 2
353378
354- FigureCanvasBase .button_press_event (self , x , y , num , dblclick = dblclick , guiEvent = event )
379+ FigureCanvasBase .button_press_event (self , x , y , num , dblclick = dblclick ,
380+ guiEvent = event , modifiers = modifiers )
355381
356- def button_dblclick_event (self ,event ):
382+ def button_dblclick_event (self , event ):
357383 self .button_press_event (event ,dblclick = True )
358384
359385 def button_release_event (self , event ):
@@ -404,37 +430,19 @@ def _get_key(self, event):
404430 else :
405431 key = None
406432
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- # In general, the modifier key is excluded from the modifier flag,
413- # however this is not the case on "darwin", so double check that
414- # we aren't adding repeat modifier flags to a modifier key.
415- if sys .platform == 'win32' :
416- modifiers = [(17 , 'alt' , 'alt' ),
417- (2 , 'ctrl' , 'control' ),
418- ]
419- elif sys .platform == 'darwin' :
420- modifiers = [(3 , 'super' , 'super' ),
421- (4 , 'alt' , 'alt' ),
422- (2 , 'ctrl' , 'control' ),
423- ]
424- else :
425- modifiers = [(6 , 'super' , 'super' ),
426- (3 , 'alt' , 'alt' ),
427- (2 , 'ctrl' , 'control' ),
428- ]
429-
430433 if key is not None :
431434 # note, shift is not added to the keys as this is already accounted for
432- for bitmask , prefix , key_name in modifiers :
435+ for bitmask , prefix , key_name in self . MOD_KEYS :
433436 if event .state & (1 << bitmask ) and key_name not in key :
434437 key = '{0}+{1}' .format (prefix , key )
435438
436439 return key
437440
441+ def _get_modifiers (self , event ):
442+ modifiers = {prefix for bitmask , prefix , key_name in self .MOD_KEYS
443+ if event .state & (1 << bitmask )}
444+ return modifiers
445+
438446 def key_press (self , event ):
439447 key = self ._get_key (event )
440448 FigureCanvasBase .key_press_event (self , key , guiEvent = event )
0 commit comments