key events directly on wx.Dialog object

259 views
Skip to first unread message

FranzSt

unread,
Jul 23, 2010, 6:18:04 PM7/23/10
to wxpytho...@lists.wxwidgets.org
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()


FranzSt

unread,
Jul 24, 2010, 4:42:28 AM7/24/10
to wxpytho...@lists.wxwidgets.org
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)


Steven Sproat

unread,
Jul 24, 2010, 9:55:31 AM7/24/10
to wxpytho...@googlegroups.com
try setting self.SetFocus() first?

--
Steven Sproat, BSc
http://whyteboard.org/

FranzSt

unread,
Jul 24, 2010, 10:24:08 AM7/24/10
to wxpytho...@lists.wxwidgets.org
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! ;)


Mike Driscoll

unread,
Jul 26, 2010, 10:11:57 AM7/26/10
to wxPython-users
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.

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

Blog: http://blog.pythonlibrary.org
Reply all
Reply to author
Forward
0 new messages