Capturing keydown events

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

Orest Kozyar wrote:

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.

Try using the wx.WANTS_CHARS style on the canvas too.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Hi Robin,

Thanks for the suggestion. I tried that, but the FigureCanvasWxAgg does
not seem to accept "style" as a keyword. Since this canvas is embedded
in a wx.Panel, would there be any way of preventing the canvas from
receiving focus and capturing user input at the panel level? I think I
tried that once by using SetFocus() which did not seem to work either.

Orest

···

-----Original Message-----
From: Robin Dunn [mailto:robin@alldunn.com]
Sent: Friday, September 21, 2007 2:47 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] Capturing keydown events

Orest Kozyar wrote:

> 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.

Try using the wx.WANTS_CHARS style on the canvas too.

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Force it eat that style with SetWindowStyle :slight_smile:
self.canvas.SetWindowStyle(wx.WANTS_CHARS
)
Docs say you might also need to call a Refresh() afterwards.

Peter.

P.S.
Today seams to be National Thank You Day in US and even if I’m not from US I would still like to thank everybody who helped me on this list over the years.

Thank you very much!

···

On 9/24/07, Buran, Brad Brad_Buran@meei.harvard.edu wrote:

I tried that, but the FigureCanvasWxAgg does
not seem to accept “style” as a keyword.


There is NO FATE, we are the creators.

Uhm, this reminds me when I got a fine for 5 mph speeding in Vermont:
after all the reprimands, the controls and the fine itself, I got a
"thank you very much sir, have a nice day". (!)

Jokes apart, I'd like to join Peter with my personal thanks for all
the beautiful things I learnt from this list in the past, and a
special one to Robin for wxPython, a toolkit light-years away from all
the others :smiley: :smiley: (Java gives you jitters? Relax with wxPython!)

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

···

On 9/24/07, Peter Damoc wrote:

P.S.
Today seams to be National Thank You Day in US and even if I'm not from US I
would still like to thank everybody who helped me on this list over the
years.
Thank you very much!

Buran, Brad wrote:

Hi Robin,

Thanks for the suggestion. I tried that, but the FigureCanvasWxAgg does
not seem to accept "style" as a keyword.

That is easily fixed, send them a bug report and/or patch. You can try setting the style after the canvas is created, but I'm not sure if that style is one of those that need to be done at creation time.

Since this canvas is embedded
in a wx.Panel, would there be any way of preventing the canvas from
receiving focus and capturing user input at the panel level? I think I
tried that once by using SetFocus() which did not seem to work either.

No, when panels and other container windows get the focus they always transfer it to the first child that can accept focus, if there is one.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!