Robin Dunn wrote:
From: Robin Dunn <robin@alldunn.com>
Subject: Re: [wxPython-users] Enter on dialogs w/ wxMac
Message-ID: <43E68671.5030505@alldunn.com>IxokaI wrote:
How can I do that? I tried in the example code calling:
button.SetFocus()
and the behavior doesn't change.
Because, as I said, buttons can't get the focus on the Mac. You need to
have a control on the dialog that can get the focus. If you just want
to display a message and an OK button then consider using the standard
wx.MessageDialog. Since it is based on a native API it does handle the
Enter and ESC keys as you would expect.
For the cases where you need either a Cancel button more control over the layout, try adding a binding to a method that captures the keystrokes and closes the dialog. Otherwise, it lets the event go.
To a sub-class of wx.Dialog, add to the __init__ method a binding:
self.Bind(wx.EVT_CHAR, self.OnChar)
Then, add the bound method to the class:
def OnChar(self, event):
key = event.KeyCode()
self.log.WriteText("You pressed keycode: %s\n" % key)
if event.MetaDown() or event.HasModifiers() or event.ShiftDown():
event.Skip()
return
if key in (13): # Add additional key codes to the tuple
self.EndModal(wx.ID_OK)
return
This will skip most keystrokes, but return when the Enter key -- without any modifiers -- is pressed. Playing around with this method should handle most circumstances.
You can try this out in the wxPython demo by modifying the Dialog example. I changed the __init__ signature of the TestDialog class to take the log, so that the OnChar method can print it out:
class TestDialog(wx.Dialog):
def __init__(
self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition,
style=wx.DEFAULT_DIALOG_STYLE, log=None
):
self.log = log
···
______________
John Jackson