Chris P.
Chris P.

Reputation: 33

Wxpython, how to create wx.KeyEvent with given key code?

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.

Upvotes: 3

Views: 1301

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22433

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.

Upvotes: 2

Related Questions