2013-08-09 16 views
6

Ho un matplotlib e ho creato un button_press_event come questo:matplotlib: come si fa a fare clic su shift?

self.fig.canvas.mpl_connect('button_press_event', self.onClick) 

def onClick(self, event) 
    if event.button == 1: 
     # draw some artists on left click 

    elif event.button == 2: 
     # draw a vertical line on the mouse x location on wheel click 

    elif event.button == 3: 
     # clear artists on right click 

Ora è possibile modificare il gestore ghiera cliccabile a qualcosa di simile

elif event.button == 2 or (event.button == 1 and event.key == "shift"): 
     # draw a vertical line on the mouse x location 
     # on wheel click or on shift+left click 
     # (alternative way if there is no wheel for example) 

Sembra che button_press_event non lo fa i tasti di supporto e key_press_event non supportano i clic del pulsante del mouse, ma non sono sicuro.

C'è un modo?

risposta

9

È possibile anche associare un tasto premuto e eventi di rilascio chiave e fare qualcosa di simile:

self.fig.canvas.mpl_connect('key_press_event', self.on_key_press) 
self.fig.canvas.mpl_connect('key_release_event', self.on_key_release) 

... 

def on_key_press(self, event): 
    if event.key == 'shift': 
     self.shift_is_held = True 

def on_key_release(self, event): 
    if event.key == 'shift': 
     self.shift_is_held = False 

Quindi è possibile controllare nella funzione onClick se self.shift_is_held.

if event.button == 3: 
    if self.shift_is_held: 
     do_something() 
    else: 
     do_something_else() 
+0

sembra una buona idea e soluzione fino a quando viene implementato un supporto adeguato per i tasti modificatori con i pulsanti del mouse. Grazie Viktor! – NotNone

0

Se siete su Qt allora si può semplicemente utilizzare:

def onClick(self, event) 
    # queries the pressed modifier keys 
    mods = QGuiApplication.queryKeyboardModifiers() 
    if event.button == 1 mods == Qt.ShiftModifier: 
     # do something when shift-clicking 
Problemi correlati