[wxPython] Sending events programmaticaly

Hello all,

I am trying to test my widgets automatically, but I don´t know how to send a key event programmatically to a window.

Suposse i want to imitate the sending of a keystroke to a text control. I would like to say something like

SendKey(txtCtrl, keyCode=48, altDown=0, shitDown=0 ,…)

Do you get the idea?

I can´t use SetValue(), because it don´t raises the EVT_CHAR event, and i would like to test it (like EVT_TEXT, and so on)

(I think i am going to have the same problem with mouse events)

Can anybody help me?

Thanks in advance

Josu Oyanguren

Josu Oyanguren writes:

Hello all,

I am trying to test my widgets automatically, but I don?t know how to send a key event programmatically to a window.

You can use wxPostEvent(recipient, event) to send events.

I am trying to test my widgets automatically, but I don´t know how
to send a key event programmatically to a window.

Suposse i want to imitate the sending of a keystroke to a text
control. I would like to say something like

...
SendKey(txtCtrl, keyCode=48, altDown=0, shitDown=0 ,...)
...

As was mentioned you can do

    wxPostEvent(window.GetEventHandler(), event)

and the event will be sent to the window in idle time, or you can do

    window.GetEventHandler().ProcessEvent(event)

and the event will be processed immediately. There are some complications
though... As mentioned in the last message the public data members of
wxKeyEvent (and wxMouseEvent) aren't currently exposed to wxPython, only the
methods.

Also, for some (most?) event types artificially generating and sending an
event won't actually effect the window. You'll be able to test your event
handlers, but the default behaviour won't happen. For an example look at
what happens when a key event happens naturally on MSW:

1. Windows sends a WM_CHAR message to a wxWindow
2. the wxWindow catches the message and turns it into a wxKeyEvent
3. the event is sent with GetEventHandler()->ProcessEvent(event) which looks
for an event handler bound with EVT_CHAR
4. if the event was not caught or the handler called event.Skip() then the
original message is passed on to the window's default WndProc where is does
things like adding a character to a text control

When you artificially generate and send an event you can simulate up to #3,
but when ProcessEvent returns there is no way to send the message on to the
window for default processing. Even if there was a way it would be
drastically
different on different platforms.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxpython.org Java give you jitters?
http://wxpros.com Relax with wxPython!

Thanks Robin,

These are the two things i didn´t understand.

As you said in other post, in next release there will be access to data
members. Do you think there will be a workaround for the other issue?

Josu Oyanguren.

···

----- Original Message -----
From: Robin Dunn <robin@alldunn.com>
To: wxPython-users <wxpython-users@wxwindows.org>
Sent: Friday, September 08, 2000 6:29 AM
Subject: Re: [wxPython] Sending events programmaticaly

I am trying to test my widgets automatically, but I don´t know how
to send a key event programmatically to a window.

Suposse i want to imitate the sending of a keystroke to a text
control. I would like to say something like

...
SendKey(txtCtrl, keyCode=48, altDown=0, shitDown=0 ,...)
...

As was mentioned you can do

    wxPostEvent(window.GetEventHandler(), event)

and the event will be sent to the window in idle time, or you can do

    window.GetEventHandler().ProcessEvent(event)

and the event will be processed immediately. There are some complications
though... As mentioned in the last message the public data members of
wxKeyEvent (and wxMouseEvent) aren't currently exposed to wxPython, only the
methods.

Also, for some (most?) event types artificially generating and sending an
event won't actually effect the window. You'll be able to test your event
handlers, but the default behaviour won't happen. For an example look at
what happens when a key event happens naturally on MSW:

1. Windows sends a WM_CHAR message to a wxWindow
2. the wxWindow catches the message and turns it into a wxKeyEvent
3. the event is sent with GetEventHandler()->ProcessEvent(event) which looks
for an event handler bound with EVT_CHAR
4. if the event was not caught or the handler called event.Skip() then the
original message is passed on to the window's default WndProc where is does
things like adding a character to a text control

When you artificially generate and send an event you can simulate up to #3,
but when ProcessEvent returns there is no way to send the message on to the
window for default processing. Even if there was a way it would be
drastically
different on different platforms.

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxpython.org Java give you jitters?
http://wxpros.com Relax with wxPython!

_______________________________________________
wxPython-users mailing list wxPython-users@wxwindows.org
http://wxwindows.org/mailman/listinfo/wxpython-users