egbert
September 8, 2006, 1:03pm
1
Hello,
I have some TextCtrl's, and I want to set focus
to the next one when I hit Enter. Like TAB.
I found this code, only the crucial line is missing.
How do i set focus to the next one in the focus order ?
self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
def OnKey(self, event):
if event.GetKeyCode() == wx.WXK_RETURN and not self.IsMultiLine():
# set focus on next item
else:
event.Skip()
Thanks,
egbert
···
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
Hello Egbert,
I have some TextCtrl's, and I want to set focus to the next
one when I hit Enter. Like TAB.
You can try something like that (untested, may not work ):
def OnKey(self, event):
if event.GetKeyCode() == wx.WXK_RETURN and not self.IsMultiLine():
event.SetKeyCode(wx.WXK_TAB)
forward = not event.ShiftDown()
ne = wx.NavigationKeyEvent()
ne.SetDirection(forward)
ne.SetCurrentFocus(self)
ne.SetEventObject(self)
self.GetParent().GetEventHandler().ProcessEvent(ne)
event.Skip()
HTH.
Andrea.
···
_________________________________________
Andrea Gavana (gavana@kpo.kz)
Reservoir Engineer
KPDL
4, Millbank
SW1P 3JA London
Direct Tel: +44 (0) 20 717 08936
Mobile Tel: +44 (0) 77 487 70534
Fax: +44 (0) 20 717 08900
Web: http://xoomer.virgilio.it/infinity77
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
egbert a écrit :
Hello, I have some TextCtrl's, and I want to set focus to the next one when I hit Enter. Like TAB.
I found this code, only the crucial line is missing.
How do i set focus to the next one in the focus order ?
self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
def OnKey(self, event):
if event.GetKeyCode() == wx.WXK_RETURN and not self.IsMultiLine():
# set focus on next item else: event.Skip()
Thanks,
egbert
I use the next :
if evt.GetKeyCode() == wx.WXK_RETURN :
obj = evt.GetEventObject()
try :
tmp = obj.IsSingleLine()
except :
tmp = True
if tmp :
tab = wx.NavigationKeyEvent()
tab.SetDirection(wx.WXK_TAB)
obj.GetParent().GetEventHandler().ProcessEvent(tab)
return
evt.Skip()
Friendly.
···
--
Hugues JEAN-BAPTISTE (hjb@agorinfo.fr)
AGORINFO S.A.S. (http://www.agorinfo.fr )
Gavana, Andrea wrote:
Hello Egbert,
I have some TextCtrl's, and I want to set focus to the next one when I hit Enter. Like TAB.
You can try something like that (untested, may not work :-D ):
def OnKey(self, event):
if event.GetKeyCode() == wx.WXK_RETURN and not self.IsMultiLine():
event.SetKeyCode(wx.WXK_TAB)
forward = not event.ShiftDown()
ne = wx.NavigationKeyEvent()
ne.SetDirection(forward)
ne.SetCurrentFocus(self)
ne.SetEventObject(self)
self.GetParent().GetEventHandler().ProcessEvent(ne)
event.Skip()
Andrea’s solution (assuming it works – I haven’t tried it either)
simulates hitting the Tab key. If you have some reason for violating
the tab order, or
you have a convenient list of the components you want to move between,
it may be easier simply to identify the target control and call
SetFocus().
Kent
egbert
September 8, 2006, 9:39pm
5
I have simplified (!) and integrated your suggestions:
def OnKey(self, event):
if event.GetKeyCode() == wx.WXK_RETURN:
try:
tmp = self.IsSingleLine()
except:
tmp = False
if tmp:
forward = not event.ShiftDown()
actie = wx.NavigationKeyEvent()
actie.SetDirection(forward)
self.GetParent().GetEventHandler().ProcessEvent(actie)
return
event.Skip()
It seems to work fine, the Shift-Enter as well.
Thanks a lot.
egbert
···
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
Robin
September 8, 2006, 9:58pm
6
Gavana, Andrea wrote:
Hello Egbert,
I have some TextCtrl's, and I want to set focus to the next one when I hit Enter. Like TAB.
You can try something like that (untested, may not work ):
def OnKey(self, event):
if event.GetKeyCode() == wx.WXK_RETURN and not self.IsMultiLine():
event.SetKeyCode(wx.WXK_TAB)
forward = not event.ShiftDown()
ne = wx.NavigationKeyEvent()
ne.SetDirection(forward)
ne.SetCurrentFocus(self)
ne.SetEventObject(self)
self.GetParent().GetEventHandler().ProcessEvent(ne)
event.Skip()
There is now a convenience method that will make and send the navigation event for you. Take a look at the docs for wx.Window.Navigate.
···
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
egbert
September 10, 2006, 8:39am
7
The docs are too concise for me.
Can you show how to do it in this case,
ie let RETURN function like TAB in single line TextCtrl's ?
egbert
···
On Fri, Sep 08, 2006 at 02:58:42PM -0700, Robin Dunn wrote:
There is now a convenience method that will make and send the navigation
event for you. Take a look at the docs for wx.Window.Navigate.
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
Robin
September 11, 2006, 7:38pm
8
egbert wrote:
There is now a convenience method that will make and send the navigation event for you. Take a look at the docs for wx.Window.Navigate.
The docs are too concise for me.
Can you show how to do it in this case, ie let RETURN function like TAB in single line TextCtrl's ?
import wx
class ReturnIsTabTextCtrl(wx.TextCtrl):
def __init__(self, *args, **kw):
wx.TextCtrl.__init__(self, *args, **kw)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
def OnKeyDown(self, evt):
key = evt.GetKeyCode()
if key == wx.WXK_RETURN:
flags = wx.NavigationKeyEvent.IsForward
if evt.ShiftDown():
flags = wx.NavigationKeyEvent.IsBackward
self.Navigate(flags)
else:
evt.Skip()
class Test(wx.Frame):
def __init__(self, *args, **kw):
wx.Frame.__init__(self, *args, **kw)
sizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
p = wx.Panel(self)
for i in range(10):
tc = ReturnIsTabTextCtrl(p, size=(150,-1))
tc.SetValue(chr(ord('a') + i) * 5)
sizer.Add(tc, flag=wx.ALL, border=5)
border = wx.BoxSizer()
border.Add(sizer, 0, wx.ALL, 10)
p.SetSizer(border)
app = wx.App(False)
frm = Test(None, title="Return is Tab Test")
frm.Show()
app.MainLoop()
···
On Fri, Sep 08, 2006 at 02:58:42PM -0700, Robin Dunn wrote:
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
egbert
September 12, 2006, 8:55am
9
Paul and Robin,
Thanks for your clear examples. Now I do understand the docs.
egbert
···
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
Robin Dunn wrote:
<-cut code->
Only for say that the line:
key = evt.GetKeyCode()
if key == wx.WXK_RETURN:
need to become:
key = evt.GetKeyCode()
if key in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER):
for true compatibility
Thanks for the code!
Michele