[wxPython] Capturing the ENTER key event in a wxTextCtrl

981 views
Skip to first unread message

Peter Moscatt

unread,
Sep 24, 2001, 11:54:02 AM9/24/01
to wxpytho...@lists.wxwindows.org
How can I capture the ENTER key event (when it's
pressed) then simulate the TAB key so when I ender
data into a wxTextCtrl then press ENTER it will then
jump to the next control ?

Pete


__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger. http://im.yahoo.com

Donnal Walter

unread,
Sep 24, 2001, 5:43:01 PM9/24/01
to wxpytho...@lists.wxwindows.org
--- Peter Moscatt <pmos...@yahoo.com> wrote:
> How can I capture the ENTER key event (when it's
> pressed) then simulate the TAB key so when I ender
> data into a wxTextCtrl then press ENTER it will then
> jump to the next control ?

Try the following code and see if it behaves as you intend. Ether
TAB or ENTER will process the text and go on to the next textctrl.
If this is what you want, you will need to look up Surviving with
wxEVT_KILL_FOCUS under Microsoft Windows at:

http://wxpython.org/cgi-bin/wiki/wxPython_20Cookbook

from wxPython.wx import *

class MyFrame(wxFrame):

def __init__(self, parent=None, id=-1, title='Process Tab'):
wxFrame.__init__(self, parent, id, title)
panel = wxPanel(self, -1)
t1 = wxTextCtrl(panel, 10, '',
wxDefaultPosition,
wxDefaultSize)
t2 = wxTextCtrl(panel, 20, '',
wxDefaultPosition,
wxDefaultSize)
t3 = wxTextCtrl(panel, 30, '',
wxDefaultPosition,
wxDefaultSize)
EVT_KILL_FOCUS(t1, self.OnLeave)
EVT_KILL_FOCUS(t2, self.OnLeave)
EVT_KILL_FOCUS(t3, self.OnLeave)
# the rest of this is to control the frame layout
box0 = wxBoxSizer(wxVERTICAL)
box0.Add(t1, 0, wxEXPAND)
box0.Add(t2, 0, wxEXPAND)
box0.Add(t3, 0, wxEXPAND)
panel.SetSizer(box0)
panel.Layout()
panel.SetAutoLayout(true)
box0.Fit(self)
box0.SetSizeHints(self)

def OnLeave(self, event):
t = event.GetEventObject()
print ('Text: %s\n' % t.GetValue())

class MyApp(wxApp):
def OnInit(self):
frame = MyFrame()
frame.Show(true)
self.SetTopWindow(frame)
return true

app = MyApp(0)
app.MainLoop()


=====
Donnal Walter
Arkansas Children's Hospital

Peter Moscatt

unread,
Sep 24, 2001, 6:12:01 PM9/24/01
to wxpytho...@lists.wxwindows.org
Hi Donnal,

Is this likely to work under Linux as well ??

Pete

> _______________________________________________
> wxpython-users mailing list
> wxpytho...@lists.wxwindows.org
>
http://lists.wxwindows.org/mailman/listinfo/wxpython-users

Robin Dunn

unread,
Sep 24, 2001, 8:12:01 PM9/24/01
to wxpytho...@lists.wxwindows.org
> How can I capture the ENTER key event (when it's
> pressed) then simulate the TAB key so when I ender
> data into a wxTextCtrl then press ENTER it will then
> jump to the next control ?
>

Bind EVT_KEY_DOWN to the text ctrl to catch the RETURN key, then you can
either call SetFocus on the control you want to move to, or perhaps try to
create and send a wxNavigationKeyEvent. Maybe something like this:

def OnKeyDown(self, evt):
if evt.GetKeyCode() != WXK_RETURN:
evt.Skip()
return

win = evt.GetEventObject()
nav = wxNavigationKeyEvent()
nav.SetDirection(true) # means to go forward
nav.SetEventObject(win)
nav.SetCurrentFocus(win)
win.GetEventHandler.ProcessEvent(nav)


I don't know if it will work, but it's worth a try.

--
Robin Dunn
Software Craftsman
ro...@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!


Donnal Walter

unread,
Sep 24, 2001, 10:59:01 PM9/24/01
to wxpytho...@lists.wxwindows.org
--- Peter Moscatt <pmos...@yahoo.com> wrote:
> Is this likely to work under Linux as well ??

AFAIK it should work fine under Linux WITHOUT the workaround for
the Windows problem with wxEVENT_KILL_FOCUS. Unfortunately I don't
have a way to test the code on Linux at the moment. Please let me
know.

> --- Donnal Walter <donnal...@yahoo.com> wrote:
>> --- Peter Moscatt <pmos...@yahoo.com> wrote:
>>> How can I capture the ENTER key event (when it's
>>> pressed) then simulate the TAB key so when I ender
>>> data into a wxTextCtrl then press ENTER it will then
>>> jump to the next control ?
>>
>> Try the following code and see if it behaves as you

>> intend. Either


>> TAB or ENTER will process the text and go on to the
>> next textctrl.
>> If this is what you want, you will need to look up
>> Surviving with
>> wxEVT_KILL_FOCUS under Microsoft Windows at:
>>
>> http://wxpython.org/cgi-bin/wiki/wxPython_20Cookbook
>>

>> ###########################################################


>> from wxPython.wx import *
>>
>> class MyFrame(wxFrame):
>>
>> def __init__(self, parent=None, id=-1,
>> title='Process Tab'):
>> wxFrame.__init__(self, parent, id, title)
>> panel = wxPanel(self, -1)
>> t1 = wxTextCtrl(panel, 10, '',
>> wxDefaultPosition,
>> wxDefaultSize)
>> t2 = wxTextCtrl(panel, 20, '',
>> wxDefaultPosition,
>> wxDefaultSize)
>> t3 = wxTextCtrl(panel, 30, '',
>> wxDefaultPosition,
>> wxDefaultSize)
>> EVT_KILL_FOCUS(t1, self.OnLeave)
>> EVT_KILL_FOCUS(t2, self.OnLeave)
>> EVT_KILL_FOCUS(t3, self.OnLeave)

>> # the rest is to control the frame layout

eightbits

unread,
Sep 23, 2014, 5:07:05 PM9/23/14
to wxpytho...@googlegroups.com
Yes, an old post , I know. But, it seems to use some syntax associated with
Windows ?
I am using Linux (Ubuntu 12.04) and wonder if this post from Robin works
with Linux.
Also, is there a method or way to allow both the TAB and the Enter key to
work in the same fashion?
Thanks.



--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/wxPython-Capturing-the-ENTER-key-event-in-a-wxTextCtrl-tp2275914p5722629.html
Sent from the wxPython-users mailing list archive at Nabble.com.

Nathan McCorkle

unread,
Sep 23, 2014, 5:31:33 PM9/23/14
to wxpytho...@googlegroups.com


On Tuesday, September 23, 2014 2:07:05 PM UTC-7, EightBits wrote:
Yes, an old post , I know. But, it seems to use some syntax associated with
Windows ?
I am using Linux (Ubuntu 12.04) and wonder if this post from Robin works
with Linux.
Also, is there a method or way to allow both the TAB and the Enter key to
work in the same fashion?

All Robin really said was to use SetFocus... I tried something like this recently and since my text fields were created in order I was able to simply do this in the TE_PROCESS_ENTER handler:
event.GetEventObject().GetNextSibling().SetFocus()


What do you really want to do? I don't understand if you want TAB to get processed like an ENTER key does (firing an event) or rather you want a TAB character to show up in your textbox when TAB is pressed, and a newline when ENTER is pressed.
Reply all
Reply to author
Forward
0 new messages