Problem getting keypresses within a panel

I want my simple application to be able to receive keypresses. The
following code fails to show the keypresses:

#!/usr/bin/pythonw -i
import wx

app = wx.PySimpleApp()
  
def OnKeyDown(event):
    print "key %c"%event.GetKeyCode()
  
frame=wx.Frame(None)
panel=wx.Panel(frame)
panel.Bind(wx.EVT_KEY_DOWN,OnKeyDown)
  
text=wx.StaticText(panel,label="My Text")

frame.Show()
app.MainLoop()

# end

If I comment out the line beginning "text=" I am able to receive
keypresses from the bare Panel. Is the StaticText is absorbing the
keypresses somehow?

System details:
OS X 10.4
Python 2.3.5 (#1, Jan 13 2006, 20:13:11)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
wx.__version__ : '2.5.3.1'

Regards,
Mark
markrages@gmail

···

--
Mark Rages, Engineer
Midwest Telecine LLC
markrages@midwesttelecine.com

Mark,

From: Mark Rages [mailto:markrages@gmail.com]
Sent: Saturday, October 06, 2007 5:37 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Problem getting keypresses within a panel

I want my simple application to be able to receive
keypresses. The following code fails to show the keypresses:

#!/usr/bin/pythonw -i
import wx

app = wx.PySimpleApp()
  
def OnKeyDown(event):
    print "key %c"%event.GetKeyCode()
  
frame=wx.Frame(None)
panel=wx.Panel(frame)
panel.Bind(wx.EVT_KEY_DOWN,OnKeyDown)
  
text=wx.StaticText(panel,label="My Text")

frame.Show()
app.MainLoop()

# end

If I comment out the line beginning "text=" I am able to
receive keypresses from the bare Panel. Is the StaticText is
absorbing the keypresses somehow?

System details:
OS X 10.4
Python 2.3.5 (#1, Jan 13 2006, 20:13:11) [GCC 4.0.1 (Apple
Computer, Inc. build 5250)] on darwin wx.__version__ : '2.5.3.1'

Regards,
Mark
markrages@gmail
--
Mark Rages, Engineer
Midwest Telecine LLC
markrages@midwesttelecine.com

If I recall correctly, StaticText controls cannot receive key events and
since it seems to be receiving focus on load up since it's the only widget
on the panel, it "eats" the key strokes. To test my hypothesis, I added
the following line right after you created the StaticText control:

panel.SetFocus()

That did the trick. You could also replace the StaticText with e TextCtrl
and you'll notice that the TextCtrl gets the focus immediately there too.

Mike

···

-----Original Message-----

Mark Rages wrote:

I want my simple application to be able to receive keypresses. The
following code fails to show the keypresses:

#!/usr/bin/pythonw -i
import wx

app = wx.PySimpleApp()
  
def OnKeyDown(event):
    print "key %c"%event.GetKeyCode()
  
frame=wx.Frame(None)
panel=wx.Panel(frame)
panel.Bind(wx.EVT_KEY_DOWN,OnKeyDown)
  
text=wx.StaticText(panel,label="My Text")

frame.Show()
app.MainLoop()

# end

If I comment out the line beginning "text=" I am able to receive
keypresses from the bare Panel. Is the StaticText is absorbing the
keypresses somehow?

System details:
OS X 10.4
Python 2.3.5 (#1, Jan 13 2006, 20:13:11)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
wx.__version__ : '2.5.3.1'

Regards,
Mark
markrages@gmail

Only the widget that has the keyboard focus will get key events, and since it is not a command event it doesn't propagate up to the parent widgets. Also panels with focusable children will never keep the focus for themselves.

See also: self.Bind vs. self.button.Bind - wxPyWiki

···

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

Adding the SetFocus() works for wxGTK (installed with fink) but not for wxMac.

Regards,
Mark
markrages@gmail

···

On 10/8/07, Mike Driscoll <mdriscoll@co.marshall.ia.us> wrote:

Mark,

If I recall correctly, StaticText controls cannot receive key events and
since it seems to be receiving focus on load up since it's the only widget
on the panel, it "eats" the key strokes. To test my hypothesis, I added
the following line right after you created the StaticText control:

panel.SetFocus()

That did the trick. You could also replace the StaticText with e TextCtrl
and you'll notice that the TextCtrl gets the focus immediately there too.

--
Mark Rages, Engineer
Midwest Telecine LLC
markrages@midwesttelecine.com

Sorry about that. I don't have a Mac to test with, so I was unaware. I was
also unaware that you needed that functionality. I primarily use Windows,
although I am getting into more and more Linux as time goes by.

Mike

···

-----Original Message-----
From: Mark Rages [mailto:markrages@gmail.com]
Sent: Monday, October 08, 2007 2:25 PM
To: Mike Driscoll
Subject: Re: Problem getting keypresses within a panel

On 10/8/07, Mike Driscoll <mdriscoll@co.marshall.ia.us> wrote:
> Mark,
>
>
> If I recall correctly, StaticText controls cannot receive
key events
> and since it seems to be receiving focus on load up since it's the
> only widget on the panel, it "eats" the key strokes. To test my
> hypothesis, I added the following line right after you
created the StaticText control:
>
> panel.SetFocus()
>
> That did the trick. You could also replace the StaticText with e
> TextCtrl and you'll notice that the TextCtrl gets the focus
immediately there too.
>

Adding the SetFocus() works for wxGTK (installed with fink)
but not for wxMac.

Regards,
Mark
markrages@gmail
--
Mark Rages, Engineer
Midwest Telecine LLC
markrages@midwesttelecine.com

So is it possible for a wxPanel to have keyboard focus in the Mac port?

Regards,
Mark
markrages@gmail

···

On 10/8/07, Robin Dunn <robin@alldunn.com> wrote:

Only the widget that has the keyboard focus will get key events, and
since it is not a command event it doesn't propagate up to the parent
widgets. Also panels with focusable children will never keep the focus
for themselves.

See also: self.Bind vs. self.button.Bind - wxPyWiki

--
Mark Rages, Engineer
Midwest Telecine LLC
markrages@midwesttelecine.com

Mark Rages wrote:

···

On 10/8/07, Robin Dunn <robin@alldunn.com> wrote:

Only the widget that has the keyboard focus will get key events, and
since it is not a command event it doesn't propagate up to the parent
widgets. Also panels with focusable children will never keep the focus
for themselves.

See also: self.Bind vs. self.button.Bind - wxPyWiki

So is it possible for a wxPanel to have keyboard focus in the Mac port?

Probably not if it has child widgets. If all you need are some static texts then you can easily draw them yourself in the EVT_PAINT handler of the panel...

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

I'd like to see a quick example of this, if you have the time and inclination.

My application is an embedded device. It will be controlled by a few
keypresses. There is no pointer device, just a minimal keyboard. To
make the best use of the limited screen real estate, a couple of the
buttons are designated for "next" and "previous" screens, while the
other buttons may vary in function per-screen. My implementation has
a wxFrame willing the display. Each screen is a panel, which can be
put into the wxFrame's sizer as desired. The panel will catch events
from the keys. I would appreciate any comments about this design.

The device uses wxGTK, not wxMac, so the wxGTK behavior will work for
me, but I have a bad feeling about relying on platform-specific
behavior for core functionality.

Regards,
Mark
markrages@gmail

···

On 10/8/07, Robin Dunn <robin@alldunn.com> wrote:

Mark Rages wrote:
> So is it possible for a wxPanel to have keyboard focus in the Mac port?

Probably not if it has child widgets. If all you need are some static
texts then you can easily draw them yourself in the EVT_PAINT handler of
the panel...

--
Mark Rages, Engineer
Midwest Telecine LLC
markrages@midwesttelecine.com

Mark Rages wrote:

···

On 10/8/07, Robin Dunn <robin@alldunn.com> wrote:

Mark Rages wrote:

So is it possible for a wxPanel to have keyboard focus in the Mac port?

Probably not if it has child widgets. If all you need are some static
texts then you can easily draw them yourself in the EVT_PAINT handler of
the panel...

I'd like to see a quick example of this, if you have the time and inclination.

Grep for "dc.DrawText" in the demo modules.

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