Binding keys

Ok, Porting question #1. :slight_smile:

I've got the main window of my app ported, with proper search/play/stop
stuff. The main window class is created like:

class main_window(wxFrame):
聽聽def __init__(self, parent, id, title):
聽聽聽聽wxFrame.__init__(self, parent, -1, title, size = (600, 400),
style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE|wxWANTS_CHARS)

I want to bind certain keypresses to the entire main window. For example,
when I press Escape anywhere in the main window, I want to run a StopAudio
function.

In Perl/Tk, I can do this:

$mw->bind("<Key-Escape>", [\&stop_mp3]);

In WxPython, though, I try this:

聽聽聽聽EVT_CHAR(self, self.EvtChar)

but it doesn't work. If I restrict the EVT_CHAR to a certain part of the
main window, though, it does work:

聽聽聽聽self.searchpanel = SearchPanel(self, self.db)
聽聽聽聽EVT_CHAR(self.searchpanel, self.EvtChar) <-- This works

What am I doing wrong?

Thanks,
Wade

Try using a wxAcceleratorTable.

路路路

On Tue, 9 Sep 2003 11:30:10 -0400 (EDT), H. Wade Minter <minter@lunenburg.org> wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ok, Porting question #1. :slight_smile:

I've got the main window of my app ported, with proper search/play/stop
stuff. The main window class is created like:

class main_window(wxFrame):
  def __init__(self, parent, id, title):
    wxFrame.__init__(self, parent, -1, title, size = (600, 400),
style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE|wxWANTS_CHARS)

I want to bind certain keypresses to the entire main window. For example,
when I press Escape anywhere in the main window, I want to run a StopAudio
function.

In Perl/Tk, I can do this:

$mw->bind("<Key-Escape>", [\&stop_mp3]);

In WxPython, though, I try this:

    EVT_CHAR(self, self.EvtChar)

but it doesn't work. If I restrict the EVT_CHAR to a certain part of the
main window, though, it does work:

    self.searchpanel = SearchPanel(self, self.db)
    EVT_CHAR(self.searchpanel, self.EvtChar) <-- This works

What am I doing wrong?

--
Mark

Mark Melvin wrote:

but it doesn't work. If I restrict the EVT_CHAR to a certain part of the
main window, though, it does work:

    self.searchpanel = SearchPanel(self, self.db)
    EVT_CHAR(self.searchpanel, self.EvtChar) <-- This works

What am I doing wrong?

Try using a wxAcceleratorTable.

A little, more detail: KEY and CHAR events only go to the window that
has the focus. Accelerators are the mechanism to use to have a
frame-wide hotkey. When the key is pressed when any subwindow of the
frame has the focus then a EVT_MENU event is generated (since normally
you use accelerators with menu items.)

路路路

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