Clicking in a TextCtrl, from another program, doesn't move the caret

I wanted ctrl+backspace to delete a word in my TextCtrl text box. I searched and found that I need to use wx.TE_RICH to enable this support (#10344 (Ctrl-Backspace not working in wx.TextCtrl, wxPython 2.8.9.1) – wxWidgets), so I did that, and it worked. But now, when I switch programs, and then click in my TextCtrl, from the other program, it doesn’t move the cursor. I have to click twice to move the cursor. I would expect this behavior on a mac, because that’s what macs’ have always done, but not on Windows. It didn’t do this before I added wx.TE_RICH to the TextCtrl. I also tried wx.TE_RICH2 and saw the same results. How can I get the behavior I expect?

I was able to implement the same ctrl+backspace behavior as the TE_RICH style. To check for backspace I used this:

if (event.GetKeyCode() == wx.WXK_BACK) and (event.GetModifiers() == wx.MOD_CONTROL):

Then I used self.GetValue(), self.GetInsertionPoint(), str.isalnum(), self.SetValue(), and self.SetInsertionPoint() to implement the TE_RICH behavior.

Unfortunately, I can’t use ctrl+z to undo when I use SetValue(). I tried a number of things to correct this, with no luck. I guess I’ll have to implement a custom undo/redo mechanism to do better.

I found the answer… Thanks to this Damir’s answer regarding Winforms TextBox’s here:

c# - Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word - Stack Overflow

To fix in wxPython use the AutoComplete method of wx.TextEntry, like this:

class MyAutoCompleter(wx.TextCompleter):
    def Start(self, prefix):
        return False

textCtrl = wx.TextCtrl(parent)
textCtrl.AutoComplete(MyAutoCompleter())

This should work for anything that is inherits the TextEntry class, e.g. TextCtrl or ComboBox.

Unfortunately, calling textCtrl.AutoComplete() prevents my textCtrl from receiving wx.EVT_KEY_DOWN events, which I was using to detect Tab and Shift+Tab key presses, in order to allow the user to navigate a UI.

well, I doubt catching things like wx.EVT_KEY_DOWN in a wx.TextCtrl is the proper way of navigating a GUI :face_with_hand_over_mouth: