I originally posted this message on the matplotlib mailing list, but did not
get any feedback. I thought I'd check over here to see if anyone has had a
similar issue with key focus and capturing keys. I have looked at the code
for the FigureCanvasWxAgg and it's ancestor classes all the way up to
FigureCanvasBase, but cannot see anywhere that would be blocking the
keypress events. I know that if I swap out FigureCanvasWxAgg for a TextCtrl
(just as a test), I can successfully detect the appropriate key events.
Anyway, the original message to matplotlib:
···
===================
I'm embedding a FigureCanvasWxAgg into a wx.Panel and binding key events to
it:
class MyPanel(wx.Panel)
def __init__(self, parent, file, id=-1):
wx.Panel.__init__(self, parent,
style=wx.WANTS_CHARS | wx.NO_FULL_REPAINT_ON_RESIZE)
#Set up the canvas
self.figure = Figure((9,8),75)
self.canvas = FigureCanvasWxAgg(self, -1, self.figure)
self.subplot = self.figure.add_subplot(111)
#Set up the toolbar
self.toolbar = NavigationToolbar2Wx(self.canvas)
tw, th = self.toolbar.GetSizeTuple()
fw, fh = self.canvas.GetSizeTuple()
self.toolbar.SetSize(wx.Size(fw, th))
#Set up figure manager
self.figmgr = FigureManager(self.canvas, 1, self)
self.canvas.Bind(wx.EVT_KEY_UP, self.__keyup)
self.canvas.Bind(wx.EVT_KEY_DOWN, self.__keydown)
def self.__keyup(self, evt):
print 'key up'
def self.__keydown(self, evt):
print 'key down'
The program successfully detects keydown events for just about every key
with three major exceptions: wx.WXK_RETURN, wx.WXK_RIGHT, wx.WXK_LEFT.
There may be other keys out there, but those three keys are the ones I need
to process events for.
I've tried subclassing FigureCanvasWxAgg and defining key_press_event (which
doesn't seem to work) and overriding _onKeyDown (which still does not
capture the keys I need). Something weird seems to be happening to these
keys, and was wondering if anyone could help me?
I am able to detect the EVT_KEY_UP for these three keys. Just not the key
down ones.
Thanks!
Orest