Question about EVT_CHAR/EVT_KEY_DOWN handler

20 views
Skip to first unread message

raf

unread,
Mar 11, 2025, 10:50:15 AMMar 11
to wxpytho...@googlegroups.com
Hi,

I'm converting a python-2.7+wxPython-2.8.12.1 program
to python3.12+wxPython-4.2.2. I'm testing on macos-10.14.

I have a Frame containing a Choice, and two TextCtrl,
one for a user name and one for a password.
It's a login window. For each of these controls, I do:

ctrl.Bind(EVT_CHAR, self.OnChar)

And self.OnChar is supposed to handle Ctrl-Q, Ctrl-W, Escape,
Tab and Return, and skipping anything else.

However, OnChar only gets called when Tab is pressed
(but not if Shift is down at the time), and only when
the focus is in the first TextCtrl, not the second.

So if the focus is in the first TextCtrl, Tab (via OnChar)
moves focus to the second TextCtrl, but another Tab there
does not call OnChar and the focus stays where it is.
Shift-Tab never calls OnChar, but the focus switches
between the two TextCtrl. So something is processing
some keys, but it's not my OnChar method.

I'd really like Return when in the password TextCtrl to
trigger OnChar which would effectively click the login button.

How can I get the OnChar method to be called when any key
is pressed, not just Tab? And to be called when the focus
is in any of the controls that have had OnChar bound to
EVT_CHAR, not just the first TextCtrl?

I looked at:

https://wxpython.org/Phoenix/docs/html/wx.KeyEvent.html

And saw:

To summarize: you should handle wxEVT_CHAR if you
need the translated key and wxEVT_KEY_DOWN if you
only need the value of the key itself, independent of
the current keyboard state.

So I tried binding EVT_KEY_DOWN instead of EVT_CHAR.
The behaviour changed, but not to what I was hoping for.
Now, Tab doesn't invoke OnChar, but Return does, but only
when the focus is in the first TextCtrl, not when focus
is in the second TextCtrl.

Does anyone have an idea of what I'm doing wrong?

Also, the above link says:

Another difference between key and int events is that...

without specifying what an "int event" is. What is it?
Further down, it refers to EVT_CHAR as an int event but
that constant is used with class KeyEvent. So I'm a bit
confused.

Thanks,
raf

Anders Munch

unread,
Mar 11, 2025, 11:36:26 AMMar 11
to wxpytho...@googlegroups.com
raf wrote:
> [various confusing key binding behaviours]

Two things to look out for:
- Call event.Skip() for the keys that you don't want to take over.
- Put the controls on a wx.Panel, and not directly on the wx.Frame.

I can't explain the strange behaviour, but these are the first things to check.

regards,
Anders

raf

unread,
Mar 12, 2025, 6:54:06 AMMar 12
to wxpytho...@googlegroups.com
Thanks, but I'm already doing both.

cheers,
raf

raf

unread,
Mar 12, 2025, 7:06:13 AMMar 12
to wxpytho...@googlegroups.com
> Thanks, I'm already doing both.

Actually, I see that the panel is created with:

panel = Panel(self, style=0) # Disable TAB_TRAVERSAL

It looks like style=0 suppressed automatic tab
traversal so my code could do it, but maybe this is no
longer the way to suppress default tab traversal.
Because something other than my code is doing tab
traversal and I don't want it to. I'll look into that.
Hmm, looking at:

https://wxpython.org/Phoenix/docs/html/wx.Panel.html#wx.Panel

It looks like style=0 should suppress tab traversal
which is the default for the style parameter.
But it isn't suppressed (on mac). :-(

But even if that were the problem, my event handler should
still be getting other keys like Ctrl-Q, Escape, Return.
So maybe it's not relevant. Very odd.

cheers,
raf

Reply all
Reply to author
Forward
0 new messages