3

I'm trying to create a wx.KeyEvent object, I need it to emulate key press in wx.TextCtrl. I don't want to propagate the event (if possible), just create an object. Is there a way to do this?

I've looked at wx.KeyEvent page on the wxpython.org, yet I didn't find any useful information. My only hint is that maybe I could create a wx.Event object and give it the parameters I want?

EDIT: I've tried instantiating an Event object with eventType=wx.EVT_KEY_DOWN, but I get the exception saying that it cannot be subclassed/instantiated. No wonder, cause how would I even pass parameters into it.

2
  • Creating low level events is not a good idea. Describe what you are trying to do, and we may find a better way. You can SetInsertPoint, select, edit, and what not. Commented May 8, 2019 at 17:23
  • I am trying to implement auto completion in wx.RichTextCtrl, If I remember it correctly then SetInsertionPoint couldn't help me, as I am also styling the text depending on the content. for example numbers are of different color than keywords or parantheses. I found that only EmulateKeyPress worked for me. Other forms would only work partially. Commented May 10, 2019 at 15:06

1 Answer 1

2

You can generate keys, text and mouse actions with wx.UIActionSimulator https://wxpython.org/Phoenix/docs/html/wx.UIActionSimulator.html

i.e.

keyinput = wx.UIActionSimulator()
keyinput.Char(ord("z"))
keyinput.Char(ord("\t"))

would simulate a "z" followed by a tab

Actions available are:

> Char  Press and release a key.
> KeyDown   Press a key.
> KeyUp Release a key.
> MouseClick    Click a mouse button.
> MouseDblClick Double-click a mouse button.
> MouseDown Press a mouse button.
> MouseDragDrop Perform a drag and drop operation.
> MouseMove Move the mouse to the specified coordinates.
> MouseUp   Release a mouse button.
> Text  Emulate typing in the keys representing the given string.
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! This is exactly what I needed.
@Rolf of Saxony and what if I need to do the reverse thing - simulate an EVT_BUTTON event when a hotkey press is detected?
@ivan866 Best create a question with your scenario described in detail.
Note that wxUIActionSimulator requires wxWidgets to have been built with XTest support, which is currently not the case on Debian (see: bugs.debian.org/cgi-bin/bugreport.cgi?bug=1037164).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.