TextCtrl: Emulating key events in wxPython 4

Hello again,

I am trying to test my dialogs in wxPython programmatically. For this, I am emitting the events of wxPython like this:

wx.PostEvent(element, wx.CommandEvent(wx.EVT_BUTTON._getEvtType(), element.GetId()))
wx.Yield()

Currently, I am stuck with emitting events for TextCtrls.

I have tried to...
* Post a key event.

charHookEvent = wx.KeyEvent(wx.EVT_CHAR_HOOK._getEvtType())
charHookEvent.SetEventObject(element)
event = DummyKeyEvent(wx.EVT_KEY_DOWN._getEvtType())

The corresponding function in my dialog is called, however, the keycode is 0 and I have no chance to set it (I tried to override the KeyEvent and override the methods GetKeyCode() etc.)

  • I tried to hand the KeyEvent over to .EmulateKeyPress, but this somehow does nothing.
  • I took a look at wx.UIActionSimulator. This works for me if the dialog is shown. But if the dialog is not shown, the function outputs text to my IDE (or whatever other application currently has the focus).

A third way would be to directly call SetValue, but than I may have a different behaviour of the dialog in my test. The disadvantage is, that I cannot test keyboard shortcuts like Ctrl+S etc. like this.

Is there anything I am missing?

Thank you for your help and best regards