wxDialog and Key event

Hi:

I want to capture arrow keys in wxDialog, but it does not work.
Do I make any mistake in following code?

···

===================================================

from wxPython.wx import *

class MyPanel(wxPanel):

    def __init__(self, parent, id):
    
        wxPanel.__init__(self, parent, id, size=wxSize(100,100))
        
        EVT_CHAR(self, self.OnKey)

    def OnKey(self, event):
    
        key_code=event.GetKeyCode()
        print key_code
        
class Dialog(wxDialog):

    def __init__(self, parent, id, title):

        wxDialog.__init__(self, parent, id, title, pos=wxDefaultPosition,
                         size=wxDefaultSize, style=wxDEFAULT_DIALOG_STYLE)
        MyPanel(self, -1)

class App(wxApp):

    def OnInit(self):

        dialog = Dialog(NULL, -1, "wxDialog TEST")
        dialog.Show(true)
        self.SetTopWindow(dialog)

        return true

if __name__ == '__main__':

   app = App(0)
   app.MainLoop()

FWIW, I found that moving
a) EVT_CHAR(self,self.OnKey)
b) the OnKey definition (def OnKey)
into class Dialog works.
David

···

----- Original Message -----
From: "Thomas Moore" <jsfrank.chen@msa.hinet.net>
To: "wxPython-list" <wxPython-users@lists.wxwindows.org>
Sent: Monday, January 12, 2004 12:41 PM
Subject: [wxPython-users] wxDialog and Key event

David

Hi:

I want to capture arrow keys in wxDialog, but it does not work.
Do I make any mistake in following code?

===================================================

from wxPython.wx import *

class MyPanel(wxPanel):

    def __init__(self, parent, id):

        wxPanel.__init__(self, parent, id, size=wxSize(100,100))

        EVT_CHAR(self, self.OnKey)

    def OnKey(self, event):

        key_code=event.GetKeyCode()
        print key_code

class Dialog(wxDialog):

    def __init__(self, parent, id, title):

        wxDialog.__init__(self, parent, id, title, pos=wxDefaultPosition,
                         size=wxDefaultSize, style=wxDEFAULT_DIALOG_STYLE)
        MyPanel(self, -1)

class App(wxApp):

    def OnInit(self):

        dialog = Dialog(NULL, -1, "wxDialog TEST")
        dialog.Show(true)
        self.SetTopWindow(dialog)

        return true

if __name__ == '__main__':

   app = App(0)
   app.MainLoop()

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

David:

Yes. But the wxDialog has buttons, so I think I have to put EVT_CHAR() in
the child control. The arrow keys are get intercepted by wxDialog.

···

====================================================

from wxPython.wx import *

class MyPanel(wxPanel):

    def __init__(self, parent, id):

        wxPanel.__init__(self, parent, id, size=wxSize(100,100))

        EVT_CHAR(self, self.OnKey)

    def OnKey(self, event):

        key_code=event.GetKeyCode()
        print key_code

class Dialog(wxDialog):

    def __init__(self, parent, id, title):

        wxDialog.__init__(self, parent, id, title, pos=wxDefaultPosition,
                         size=wxSize(200,200), style=wxDEFAULT_DIALOG_STYLE)
        box=wxBoxSizer(wxVERTICAL)
        self.panel=MyPanel(self, -1)
        self.OkButton = wxButton(self, -1, "OK")
        self.CancelButton = wxButton(self, -1, "Cancel")
        box.Add(self.panel, 1, wxEXPAND)
        box1=wxBoxSizer(wxHORIZONTAL)
        box1.Add(self.OkButton,0, wxALL, 5)
        box1.Add(self.CancelButton, 0, wxALL, 5)
        box.Add(box1, 0, wxEXPAND)
        self.SetSizer(box)

        EVT_CLOSE(self, self.OnClose)
        EVT_BUTTON(self, self.OkButton.GetId(), self.OnButton)
        EVT_BUTTON(self, self.CancelButton.GetId(), self.OnButton)

    def OnButton(self, event):
        pass

    def OnClose(self, event):

        self.Destroy()

class App(wxApp):

    def OnInit(self):

        dialog = Dialog(NULL, -1, "wxDialog TEST")
        dialog.Show(true)
        self.SetTopWindow(dialog)

        return true

if __name__ == '__main__':

   app = App(0)
   app.MainLoop()

----- Original Message -----
From: "David Hassalevris" <bluepaul@earthlink.net>
To: <wxPython-users@lists.wxwindows.org>
Sent: Tuesday, January 13, 2004 5:02 AM
Subject: Re: [wxPython-users] wxDialog and Key event

FWIW, I found that moving
a) EVT_CHAR(self,self.OnKey)
b) the OnKey definition (def OnKey)
into class Dialog works.
David

----- Original Message -----
From: "Thomas Moore" <jsfrank.chen@msa.hinet.net>
To: "wxPython-list" <wxPython-users@lists.wxwindows.org>
Sent: Monday, January 12, 2004 12:41 PM
Subject: [wxPython-users] wxDialog and Key event

David

> Hi:
>
> I want to capture arrow keys in wxDialog, but it does not work.
> Do I make any mistake in following code?
>
> ===================================================
>
> from wxPython.wx import *
>
> class MyPanel(wxPanel):
>
> def __init__(self, parent, id):
>
> wxPanel.__init__(self, parent, id, size=wxSize(100,100))
>
> EVT_CHAR(self, self.OnKey)
>
> def OnKey(self, event):
>
> key_code=event.GetKeyCode()
> print key_code
>
> class Dialog(wxDialog):
>
> def __init__(self, parent, id, title):
>
> wxDialog.__init__(self, parent, id, title,

pos=wxDefaultPosition,

> size=wxDefaultSize,

style=wxDEFAULT_DIALOG_STYLE)

> MyPanel(self, -1)
>
> class App(wxApp):
>
> def OnInit(self):
>
> dialog = Dialog(NULL, -1, "wxDialog TEST")
> dialog.Show(true)
> self.SetTopWindow(dialog)
>
> return true
>
> if __name__ == '__main__':
>
> app = App(0)
> app.MainLoop()
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
> For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Thomas Moore wrote:

Hi:

I want to capture arrow keys in wxDialog, but it does not work.

EVT_CHAR, EVT_KEY_DOWN and EVT_KEY_UP will only be sent to the window or
the control that has the focus. If you need to get them when any
control has the focus then you need bind the events to each
control/window, but even then navigation keys will (depending on the
situation) may be turned into navigation events before the KEY/CHAR
handlers get them.

Another possibility for some keys is to use a wxAcceleratorTable and
then catch them as EVT_MENU events.

···

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