Getting the last entered character from a TextCtrl

John Salerno <johnjsal@[...].com> wrote:
> > Saketh Bhamidipati wrote:
> > I have a TextCtrl which has EVT_TEXT bound to a method, OnText. I need > > to check what the last entered character was in OnText. In OnText(), > > event.GetString() returns the whole TextCtrl value (as it should), but I > > just want the last entered character.
> > > > What's the best way to check the last entered character in a TextCtrl?
> > > > Thanks,
> > Saketh
> > I'm not sure about *when* you want to extract this last character, but > have you tried using the [-1] slice? That will take the last character > out of a string.

Right, but the last character in the string isn't necessarily the most
recent character typed.

The correct solution is to capture wx.EVT_KEY_DOWN, get the keycode
(delete, backspace, or otherwise), save it, .Skip() it, and capture the
later wx.EVT_TEXT . Depending on the value of the saved keypress, one
can somewhat easily determine the most recent character by finding the
cursor position (with tc.GetSelection() or tc.GetInsertionPoint() ) and
checking the last keycode saved.
  

It would seem that capturing wx.EVT_KEY_DOWN would be one approach. It may not be an
entirely complete approach, though.

Although it will work to capture characters entered in a normal fashion, what about characters
entered through copy/paste via keyboard shortcuts or via the right-click context menu?

Then again, those cases may also involve entry of multiple characters at once.

Tim Tucker
tim@timtucker.com
http://www.timtucker.com/

I don't know if you should use TextCtrl but you can try
StyledTextControl. Have a look at StyledTextControl in the wxPython
demo, in "More Windows/Controls" section. It emits modify event and
you can catch appended and deleted text events.

ยทยทยท

2006/8/14, Timothy J. Tucker <tim@timtucker.com>:

>
> John Salerno <johnjsal@[...].com> wrote:
> >
> > Saketh Bhamidipati wrote:
> > > I have a TextCtrl which has EVT_TEXT bound to a method, OnText. I need
> > > to check what the last entered character was in OnText. In OnText(),
> > > event.GetString() returns the whole TextCtrl value (as it should), but I
> > > just want the last entered character.
> > >
> > > What's the best way to check the last entered character in a TextCtrl?
> > >
> > > Thanks,
> > > Saketh
> >
> > I'm not sure about *when* you want to extract this last character, but
> > have you tried using the [-1] slice? That will take the last character
> > out of a string.
>
> Right, but the last character in the string isn't necessarily the most
> recent character typed.
>
> The correct solution is to capture wx.EVT_KEY_DOWN, get the keycode
> (delete, backspace, or otherwise), save it, .Skip() it, and capture the
> later wx.EVT_TEXT . Depending on the value of the saved keypress, one
> can somewhat easily determine the most recent character by finding the
> cursor position (with tc.GetSelection() or tc.GetInsertionPoint() ) and
> checking the last keycode saved.
>
It would seem that capturing wx.EVT_KEY_DOWN would be one approach. It
may not be an
entirely complete approach, though.

Although it will work to capture characters entered in a normal fashion,
what about characters
entered through copy/paste via keyboard shortcuts or via the right-click
context menu?

Then again, those cases may also involve entry of multiple characters at
once.

Tim Tucker
tim@timtucker.com
http://www.timtucker.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org