Key press event not registered properly by matplotlib
Question: I'm working with some image data where it is helpful to click on different
elements in the image. I would like to have "shift-click" be the triggering
mechanism for registering the cursor position.

I wrote a "click" function which does the right thing. However, when I tried
to upgrade it to "shift-click" it simply does not work. The weird thing is
that it DOES work on my coworker's computer using an older version of
Matplotlib (1.1, running on a linux box, where mine is 1.4.2, running on a
Mac. Both are running Python 2.7)

Does anyone have any idea what is going on? I'm completely out of ideas. Other
possibly relevant info is I installed Seaborn recently (and then uninstalled
it) and upgraded matplotlib (and tried to downgrade, but that failed)

**EDIT** OK, I think I understand now. Shift must be pressed and released at
least once before starting to work properly. Maybe there is a workaround to
this.

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    def shiftclick(refimage, comment=None, eq=True):
        class EventHandler:
            def __init__(self, spotlist):
                fig.canvas.mpl_connect('button_press_event', self.onpress)
                fig.canvas.mpl_connect('key_press_event', self.on_key_press)
                fig.canvas.mpl_connect('key_release_event', self.on_key_release)
                self.shift_is_held = False
            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
            def onpress(self, event):
                if event.inaxes!=ax:
                    return
                if self.shift_is_held:
                    xi, yi = (int(round(n)) for n in (event.xdata, event.ydata))
                    value = im.get_array()[xi,yi]
                    print xi, yi
                    spotlist.append((xi, yi))
        im = plt.imshow(refimage, interpolation='nearest', origin='lower')
    
        plt.title('SHIFT-click on locations')
        fig = plt.gcf()
        ax = plt.gca()
    
        spotlist = []
        handler=EventHandler(spotlist)
        plt.show()
        return spotlist
    
    
    def click(refimage, comment=None):
        class EventHandler:
            def __init__(self, spotlist):
                fig.canvas.mpl_connect('button_press_event', self.onpress)
            def onpress(self, event):
                if event.inaxes!=ax:
                    return
                xi, yi = (int(round(n)) for n in (event.xdata, event.ydata))
                value = im.get_array()[xi,yi]
                print xi, yi
                spotlist.append((xi, yi))
        im = plt.imshow(refimage, interpolation='nearest', origin='lower')
    
        fig = plt.gcf()
        ax = plt.gca()
    
        spotlist = []
        handler=EventHandler(spotlist)
        plt.show()
        return spotlist
    
    if __name__ == "__main__":
        a = np.random.random((100, 100))
        #this works
        click(a)
        #this doesn't work
        shiftclick(a)
    


Answer: OK, here was the problem. After upgrading Matplotlib, the MacOSX backend
refused to deal with event handling properly. It ignored all shift-button
presses.

Using the TKAgg backend fixed the problem, as long as shift is pressed at
least once before trying to select points. This is still undesirable.

Using the Qt4Agg backend had the best possible behavior. As long as the window
was selected, it responded to the first shift press correctly.

