Problems with EVT_KEY_DOWN

Hi,

I am trying to make a dialog and try to get all keypress and do some aktions
on keypress.

that's what i've done sofar.

class PINDialog(wx.Dialog):
    def __init__(self, parent, length=5, verify=False):
        wx.Dialog.__init__(self, parent, -1, 'Bitte PIN eingeben', size=(280,
360))

        self.verify = verify
        self.lenth = length

        panel = wx.Panel(self, -1)

        vbox = wx.BoxSizer(wx.VERTICAL)

        textbox = wx.BoxSizer()
        self.text = wx.StaticText(panel, -1, "Bitte geben Sie Ihren %d
stelligen PIN Code ein" % (length,))

        textbox.Add(wx.Panel(panel, -1), 1, wx.EXPAND, 0)
        textbox.Add(self.text, 0, wx.EXPAND | wx.ALL, 5)
        textbox.Add(wx.Panel(panel, -1), 1, wx.EXPAND, 0)

        hbox = wx.BoxSizer()

        hbox.Add(wx.Panel(panel, -1), 1, wx.ALL | wx.EXPAND, 0)
        self.images = {}
        for state in ['notset', 'set', 'verified'] :
            self.images[state] = wx.Bitmap(os.path.join('resources', 'icons',
'pinkey_'+state+'.png'), wx.BITMAP_TYPE_PNG)

        self.imgwidgets = []
        for x in range(length) : #@UnusedVariable
            widget = wx.StaticBitmap(panel, -1, self.images['notset'])
            self.imgwidgets.append(widget)
            hbox.Add(widget, 0, wx.ALL | wx.EXPAND, 5)

        hbox.Add(wx.Panel(panel, -1), 1, wx.ALL | wx.EXPAND, 0)

        btnbox = wx.StdDialogButtonSizer()
        btnbox.AddButton(wx.Button(panel, wx.ID_OK))
        btnbox.AddButton(wx.Button(panel, wx.ID_CANCEL))
        btnbox.Realize()

        vbox.Add(textbox, 0, wx.ALL | wx.EXPAND, 5)
        vbox.Add(hbox, 0, wx.ALL | wx.EXPAND, 5)
        vbox.Add(btnbox, 0, wx.ALL | wx.EXPAND, 5)

        panel.SetSizer(vbox)
        panel.SetFocus()

        wx.EVT_KEY_DOWN(self, self.keypress)

        vbox.Fit(self)

    def keypress(self, event):
        print "keypress", `event`
        pprint.pprint(dir(event))

The problem is that the keypress methode never gets called. What am I missing
here.

Regards
  Gerhard

···

--
----------------------------------------------------------------------------
Gerhard Schmidt | http://www.augusta.de/~estartu |
Fischbachweg 3 | | PGP Public Key
86856 Hiltenfingen | JabberID: estartu@augusta.de | on request
Germany | |

Hi Gerhard,

Perhaps the solution : http://groups.google.com/group/wxpython-users/browse_thread/thread/c82ea43795a7e171?pli=1

Regards

···

2011/2/8 Gerhard Schmidt estartu@augusta.de

Hi,

I am trying to make a dialog and try to get all keypress and do some aktions
on keypress.

that’s what i’ve done sofar.

class PINDialog(wx.Dialog):
def init(self, parent, length=5, verify=False):
wx.Dialog.init(self, parent, -1, ‘Bitte PIN eingeben’, size=(280,

360))

   self.verify = verify
   self.lenth  = length

   panel = wx.Panel(self, -1)

   vbox = wx.BoxSizer(wx.VERTICAL)

   textbox = wx.BoxSizer()
   self.text = wx.StaticText(panel, -1, "Bitte geben Sie Ihren %d

stelligen PIN Code ein" % (length,))

   textbox.Add(wx.Panel(panel, -1), 1, wx.EXPAND, 0)
   textbox.Add(self.text,           0, wx.EXPAND | wx.ALL, 5)
   textbox.Add(wx.Panel(panel, -1), 1, wx.EXPAND, 0)


   hbox = wx.BoxSizer()

   hbox.Add(wx.Panel(panel, -1), 1, wx.ALL | wx.EXPAND, 0)
   self.images = {}
   for state in ['notset', 'set', 'verified'] :
       self.images[state] = wx.Bitmap(os.path.join('resources', 'icons',

‘pinkey_’+state+‘.png’), wx.BITMAP_TYPE_PNG)

   self.imgwidgets = []
   for x in range(length) : #@UnusedVariable
       widget = wx.StaticBitmap(panel, -1, self.images['notset'])

       self.imgwidgets.append(widget)
       hbox.Add(widget, 0, wx.ALL | wx.EXPAND, 5)

   hbox.Add(wx.Panel(panel, -1), 1, wx.ALL | wx.EXPAND, 0)

   btnbox = wx.StdDialogButtonSizer()

   btnbox.AddButton(wx.Button(panel, wx.ID_OK))
   btnbox.AddButton(wx.Button(panel, wx.ID_CANCEL))
   btnbox.Realize()

   vbox.Add(textbox,   0, wx.ALL | wx.EXPAND, 5)
   vbox.Add(hbox,      0, wx.ALL | wx.EXPAND, 5)

   vbox.Add(btnbox,    0, wx.ALL | wx.EXPAND, 5)

   panel.SetSizer(vbox)
   panel.SetFocus()

   wx.EVT_KEY_DOWN(self, self.keypress)

   vbox.Fit(self)

def keypress(self, event):

   print "keypress", `event`
   pprint.pprint(dir(event))

The problem is that the keypress methode never gets called. What am I missing
here.

Regards
Gerhard

Gerhard Schmidt | http://www.augusta.de/~estartu |
Fischbachweg 3 | | PGP Public Key

86856 Hiltenfingen | JabberID: estartu@augusta.de | on request
Germany | |

Key and Char events are only sent to the widget that currently has the focus, and since they are not command events the events do not propagate up to the parents. (See self.Bind vs. self.button.Bind - wxPyWiki). So you either need to bind your event handlers to the widgets that may have the focus, or use some other way to implement what you want, such as giving the dialog an accelerator table.

···

On 2/8/11 7:55 AM, Gerhard Schmidt wrote:

Hi,

I am trying to make a dialog and try to get all keypress and do some aktions
on keypress.

that's what i've done sofar.

class PINDialog(wx.Dialog):
     def __init__(self, parent, length=5, verify=False):
         wx.Dialog.__init__(self, parent, -1, 'Bitte PIN eingeben', size=(280,
360))

         wx.EVT_KEY_DOWN(self, self.keypress)

     def keypress(self, event):
         print "keypress", `event`
         pprint.pprint(dir(event))

The problem is that the keypress methode never gets called. What am I missing
here.

--
Robin Dunn
Software Craftsman

I tried SetFocus() on self and panel and the Bind on self and panel. Any
combination does'nt work.

Regards
    Gerhard

···

Am 08.02.11 17:54, schrieb Laurent Capocchi:

Hi Gerhard,

Perhaps the solution :
http://groups.google.com/group/wxpython-users/browse_thread/thread/c82ea43795a7e171?pli=1

--
----------------------------------------------------------------------------
Gerhard Schmidt | http://www.augusta.de/~estartu |
Fischbachweg 3 | | PGP Public Key
86856 Hiltenfingen | JabberID: estartu@augusta.de | on request
Germany | |

Hello,

may I ask a related question?

I have an ActiveX control which I have embedded into a wx.Panel.

class ViewerPanel(wx.Panel):
    def __init__(self, *args, **kwargs):
        super(ViewerPanel, self).__init__(*args, **kwargs)
        self.viewer = None
        sizer = wx.BoxSizer(wx.VERTICAL)

        ActiveXWrapper = MakeActiveXClass(control.CADdyViewer)
         self.viewer = ActiveXWrapper( self, -1, style=wx.SUNKEN_BORDER)

        sizer.Add(self.viewer, 1, wx.EXPAND)

        self.SetSizer(sizer)
        self.SetAutoLayout(True)
        ...

Such a panel instance is embedded into a wx.MDIParentFrame object in my
code.

Now, my ActiveX control does need to handle keyboard events. In the
control's message handler I can intercept all keyboard events other than the
following keys: down, up, left, right and tab.

Can anyone give me a hint how I can modify my code so that I can see all key
events in my control's message handler? Somehow these events stopt by the wx
framework.

Best,
Johannes

Hello,

sorry, but I think that I have found the answer to my question myself. The
solution is using the style flag wx.WANTS_CHARS.

Best,
Johannes