Focusproblem?

Hi List,

Included is a small sample where I have a (small?) problem.

This runs OK, however, when I press the Escape button, directly after
the program starts, nothing happens. After one click in the window, it
terminates the program as desired, but I have tried several things, but
it looks as though the event is not sent at all?

What am I doing wrong?

Thanks in advance,
Dick Kniep

···

-------------------------------------------------
#!/usr/bin/env python
from wxPython.wx import *

modules ={}

class test(wxApp):

    def OnInit(self):

        self.frm = wxFrame(NULL,-1,"testframe",(-1,-1),(-1,-1))
        self.pnl = wxPanel(self.frm,-1)
        txt = wxTextCtrl(self.pnl, -1,pos=(20,20))
        txt.SetFocus()
        EVT_CHAR(self.pnl,self.OnChar)
        self.frm.Show(True)
        
        return True
        
    def OnChar(self,event):
        if event.m_keyCode == WXK_ESCAPE:
            self.canceled = True
            self.frm.Close()

if __name__ == '__main__':
    tstapp = test()
    tstapp.MainLoop()

Hi List,

Some more info. I can add an EVT_CHAR to the textcontrol. In that case I
do get the Escapekey. However, I also get the Tab key which is
definitely NOT what I want.

So if someone knows how to tackle this....

Kind regards
Dick

···

On Mon, 2003-07-28 at 11:19, Dick Kniep wrote:

Dick Kniep wrote:

Hi List,

Included is a small sample where I have a (small?) problem.

This runs OK, however, when I press the Escape button, directly after
the program starts, nothing happens. After one click in the window, it
terminates the program as desired, but I have tried several things, but
it looks as though the event is not sent at all?

What am I doing wrong?

You should be using an accelerator table instead of the EVT_CHAR. The accelerators can be caught if any window within the frame has the focus. The EVT_CHAR only happens if the window it is attached to has the focus.

#!/usr/bin/env python
from wxPython.wx import *

modules ={}

class test(wxApp):

     def OnInit(self):

         self.frm = wxFrame(NULL,-1,"testframe",(-1,-1),(-1,-1))
         self.pnl = wxPanel(self.frm,-1)
         txt = wxTextCtrl(self.pnl, -1,pos=(20,20))
         txt.SetFocus()

         anID = wxNewId()
         aTable = wxAcceleratorTable([
             (wxACCEL_NORMAL, WXK_ESCAPE, anID) ])
         self.frm.SetAcceleratorTable(aTable)
         EVT_MENU(self.frm, anID, self.OnESC)

         self.frm.Show(True)
         return True

     def OnESC(self, event):
         self.frm.Close()

if __name__ == '__main__':
     tstapp = test()
     tstapp.MainLoop()

···

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