EVT_CHAR

Hello all

I need to capture every underscore on a TextCtrl. My first choice was EVT_CHAR, but haven't had luck so far, for several reasons:

a) I can't bind the event to a single TextCtrl, because

    self.Bind(wx.EVT_CHAR, self.EvtChar, text1)

doesn't work. Instead I had to resort to

    self.Bind(wx.EVT_CHAR, self.EvtChar)

but that calls self.EvtChar on every TextCtrl, and besides I couldn't find a way to know which control generated the event.

b) EVT_CHAR only responds to keys which are note captured by the TextCtrl itself, like F1, F2, etc. ¿Is there a way to process underscores?

c) curiously, the TextCtrl is indifferent to the presence of wxWANTS_CHARS.

Regards

   Juan Pablo

Juan Pablo Romero wrote:

Hello all

I need to capture every underscore on a TextCtrl. My first choice was EVT_CHAR, but haven't had luck so far, for several reasons:

a) I can't bind the event to a single TextCtrl, because

   self.Bind(wx.EVT_CHAR, self.EvtChar, text1)

doesn't work. Instead I had to resort to

   self.Bind(wx.EVT_CHAR, self.EvtChar)

but that calls self.EvtChar on every TextCtrl,

EVT_CHAR is not a command event, so it doesn't propagate to the parent window. This means that you need to bind it directly to the textctrl

  text1.Bind(wx.EVT_CHAR, self.EvtChar)

and besides I couldn't find a way to know which control generated the event.

If you ever get into another situation like this where you do want to share the same handler for multiple controls then you can use event.GetEventObject to give you the control that the event was sent to.

b) EVT_CHAR only responds to keys which are note captured by the TextCtrl itself, like F1, F2, etc. ¿Is there a way to process underscores?

You don't ever get an event where event.GetKeyCode == ord('_') ?? You should. What platform are you on?

c) curiously, the TextCtrl is indifferent to the presence of wxWANTS_CHARS.

It depends on the platform. On windows it will cause events to be sent for things like arrow keys and others that normally would not be sent.

···

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

Robin Dunn wrote:

Juan Pablo Romero wrote:

Hello all

I need to capture every underscore on a TextCtrl. My first choice was EVT_CHAR, but haven't had luck so far, for several reasons:

a) I can't bind the event to a single TextCtrl, because

   self.Bind(wx.EVT_CHAR, self.EvtChar, text1)

doesn't work. Instead I had to resort to

   self.Bind(wx.EVT_CHAR, self.EvtChar)

but that calls self.EvtChar on every TextCtrl,

EVT_CHAR is not a command event, so it doesn't propagate to the parent window. This means that you need to bind it directly to the textctrl

    text1.Bind(wx.EVT_CHAR, self.EvtChar)

Thank you very much!

That solved all the issues.

    Juan Pablo