[wxPython] trapping enter key in listbox

Hello. I haven't figured out how to trap the enter key in a listbox. I
have a dialog with a listbox and the standard OK/Cancel buttons. I can
detect a double-click in the listbox, and I can detect the enter key
when the user hits the OK button, but I'd really like to detect the
enter key when the user is still in the listbox. I am running Linux.
Here is the relevant code:

from wxPython.wx import *

···

#---------------------------------------------------------------------------

class RecentChanges:
    def layoutDialog(self, lb, ok, cancel):
        box1 = wxBoxSizer(wxHORIZONTAL)
        box1.Add(lb, wxEXPAND, wxEXPAND)
        box2 = wxBoxSizer(wxVERTICAL)
        box2.Add(ok, 0, wxEXPAND | wxALL, 5)
        box2.Add(cancel, 0, wxEXPAND | wxALL, 5)
        box = wxBoxSizer(wxHORIZONTAL)
        box.Add(box1, wxEXPAND, wxEXPAND)
        box.Add(box2, 0, wxEXPAND)
        self.win.SetSizer(box)
        self.win.SetAutoLayout(true)

    def listBox(self, win, items):
        id = wxNewId()
        lb = wxListBox(win, id, wxPoint(80, 50), wxDefaultSize,
                          items, wxLB_SINGLE)
        EVT_LISTBOX_DCLICK(win, id, self.EvtListBoxDclick)
        return lb

    def chooseFile(self):
        item = self.lb.GetSelection()
        self.fn = self.recentChanges[item][0]
        self.win.EndModal(wxID_OK)

    def EvtListBoxDclick(self, event):
        self.chooseFile()

    def OnOkButton(self, event):
        self.chooseFile()

    def run(self, frame, recentChanges):
        self.win = win = wxDialog(frame, -1, "This is a wxDialog",
                                  wxDefaultPosition, wxSize(450, 200))

        self.recentChanges = recentChanges
        items = [x[0] for x in recentChanges]
        self.lb = self.listBox(win, items)

        ok = wxButton(win, wxID_OK, " OK ")
        cancel = wxButton(win, wxID_CANCEL, " Cancel ")
        ok.SetDefault()

        self.layoutDialog(self.lb, ok, cancel)

        EVT_BUTTON(win, wxID_OK, self.OnOkButton)

        self.fn = None
        val = win.ShowModal()
        if val == wxID_OK:
            return self.fn
        else:
            return None

Hello. I haven't figured out how to trap the enter key in a listbox. I
have a dialog with a listbox and the standard OK/Cancel buttons. I can
detect a double-click in the listbox, and I can detect the enter key
when the user hits the OK button, but I'd really like to detect the
enter key when the user is still in the listbox. I am running Linux.
Here is the relevant code:

When the controls are on a panel or dialog then the Enter key is usually
captured and used for navigation purposes. You can try giving the listbox a
wxWANTS_CHARS style and then catching it's EVT_KEY_DOWN event.

···

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

Robin Dunn wrote:

> Hello. I haven't figured out how to trap the enter key in a listbox. I
> have a dialog with a listbox and the standard OK/Cancel buttons. I can
> detect a double-click in the listbox, and I can detect the enter key
> when the user hits the OK button, but I'd really like to detect the
> enter key when the user is still in the listbox. I am running Linux.
> Here is the relevant code:

When the controls are on a panel or dialog then the Enter key is usually
captured and used for navigation purposes. You can try giving the listbox a
wxWANTS_CHARS style and then catching it's EVT_KEY_DOWN event.

Hmmm. Still not working. I tried this code out. I see every key come
in to EvtKeyDown but the enter key. I thought maybe the dialog is
getting the enter key first, but it doesn't seem to be calling my
OnOkButton function either.

from wxPython.wx import *

···

#---------------------------------------------------------------------------

class RecentChanges:
    def layoutDialog(self, lb, ok, cancel):
        box1 = wxBoxSizer(wxHORIZONTAL)
        box1.Add(lb, wxEXPAND, wxEXPAND)
        box2 = wxBoxSizer(wxVERTICAL)
        box2.Add(ok, 0, wxEXPAND | wxALL, 5)
        box2.Add(cancel, 0, wxEXPAND | wxALL, 5)
        box = wxBoxSizer(wxHORIZONTAL)
        box.Add(box1, wxEXPAND, wxEXPAND)
        box.Add(box2, 0, wxEXPAND)
        self.win.SetSizer(box)
        self.win.SetAutoLayout(true)

    def listBox(self, win, items):
        id = wxNewId()
        lb = wxListBox(win, id, wxPoint(80, 50), wxDefaultSize,
                          items, wxLB_SINGLE | wxWANTS_CHARS)
        EVT_LISTBOX_DCLICK(win, id, self.EvtListBoxDclick)
        EVT_KEY_DOWN(lb, self.EvtKeyDown)
        return lb

    def EvtKeyDown(self, event):
        print "foo", event.KeyCode(), WXK_RETURN
        if event.KeyCode() == WXK_RETURN:
            self.chooseFile()
        else:
            event.Skip()

    def chooseFile(self):
        item = self.lb.GetSelection()
        self.fn = self.recentChanges[item][0]
        self.win.EndModal(wxID_OK)

    def EvtListBoxDclick(self, event):
        self.chooseFile()

    def OnOkButton(self, event):
        self.chooseFile()

    def run(self, frame, recentChanges):
        self.win = win = wxDialog(frame, -1, "This is a wxDialog",
                                  wxDefaultPosition, wxSize(450, 200))

        self.recentChanges = recentChanges
        items = [x[0] for x in recentChanges]
        self.lb = self.listBox(win, items)

        ok = wxButton(win, wxID_OK, " OK ")
        cancel = wxButton(win, wxID_CANCEL, " Cancel ")
        ok.SetDefault()

        self.layoutDialog(self.lb, ok, cancel)

        EVT_BUTTON(win, wxID_OK, self.OnOkButton)

        self.fn = None
        val = win.ShowModal()
        if val == wxID_OK:
            return self.fn
        else:
            return None