key events directly on wx.Dialog object

No key event is coming through the dialog. I want to evaluate the
key events, coming directly on the dialog WITHOUT setting the
focus explicit on the textcontrol (then it works)

Folloging sample should show that:

import wx
import sys

class ScrolledMessageDialog(wx.Dialog):
    def __init__(self, parent, message, title, position = wx.DefaultPosition, size = (400, 300)):
        wx.Dialog.__init__(self, parent, -1, title, position, size, wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.THICK_FRAME | wx.RESIZE_BORDER)

        self.ID_CLOSE = 101

        self.theSizer = wx.BoxSizer(wx.VERTICAL)

        self.cmdSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.btnClose = wx.Button(self, self.ID_CLOSE, "&Close")
        self.cmdSizer.Add(self.btnClose, 0, wx.SHAPED | wx.ALIGN_CENTER)
        self.btnClose.SetDefault()

        self.txtMessage = wx.TextCtrl(self, -1, message, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE | wx.TE_READONLY)

        self.theSizer.Add(self.txtMessage, 9, wx.EXPAND)
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.SHAPED)
        self.theSizer.Add(self.cmdSizer, 0, wx.SHAPED | wx.ALIGN_CENTER)
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.SHAPED)

        self.SetAutoLayout(True)
        self.SetSizer(self.theSizer)

        #self.Bind(wx.EVT_BUTTON, self.OnbtnClose, id=self.ID_CLOSE)

        self.btnClose.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

        #whatever i want to bind to the key, it doesn't work
        self.Bind(wx.EVT_CHAR, self.OnChar)
        self.Bind(wx.EVT_KEY_DOWN, self.OnChar)

    def OnChar(self, event):
        print "onchar"
        keycode = event.GetKeyCode()
        if keycode == wx.WXK_ESCAPE:
            self.Close(1)

    def OnbtnClose(self, event):
        self.Close(1)

    def OnKeyDown(self, event):
        print "on key down"
        if event.GetKeyCode() == wx.WXK_ESCAPE:
            self.Close(1)

if __name__ == '__main__':
    app = wx.App()
    d = ScrolledMessageDialog(None, "message", "title")
    d.ShowModal()
    d.Destroy()
    app.MainLoop()

The purpose is also, i cannot close the dialog with the escape key.
Following line don't do, what I expected:

self.SetEscapeId(self.ID_CLOSE)

FranzSt wrote:

The purpose is also, i cannot close the dialog with the escape key.
Following line don't do, what I expected:

self.SetEscapeId(self.ID_CLOSE)

try setting self.SetFocus() first?

···

--
Steven Sproat, BSc

Steven Sproat wrote:

FranzSt wrote:

The purpose is also, i cannot close the dialog with the escape key.
Following line don't do, what I expected:

self.SetEscapeId(self.ID_CLOSE)

try setting self.SetFocus() first?

Cool, that work (apart from that, that I don't understand, whats
the difference, because the dialog itself should have the focus by
default; maybe its an gtk issue) thank you very much! :wink:

If the dialog behaves like a panel, then it probably puts focus on its
first focusable child rather than itself. Thus, settings focus
explicitly is called for here. If you'd had a panel on top of the
dialog, I don't think that would have worked.

An AcceleratorTable would have probably worked here too.

···

On Jul 24, 9:24 am, FranzSt <franz.steinhaeus...@gmx.at> wrote:

Steven Sproat wrote:
> FranzSt wrote:
>> The purpose is also, i cannot close the dialog with the escape key.
>> Following line don't do, what I expected:

>> self.SetEscapeId(self.ID_CLOSE)

> try setting self.SetFocus() first?

Cool, that work (apart from that, that I don't understand, whats
the difference, because the dialog itself should have the focus by
default; maybe its an gtk issue) thank you very much! :wink:

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org