simulated events

I have been trying to figure out how to simulate an event for
testing purposes. First, I made the following key event class:

class MyKeyEvent(wxKeyEvent):
    def __init__(self, window, keycode):
        wxKeyEvent.__init__(self, wxEVT_CHAR)
        self.m_eventObject = window
        self.m_id = window.GetId()
        self.window = window
        self.m_keyCode = keycode

Then elsewhere I try to simulate a key event as follows:

        ke = MyKeyEvent(self.field1, 49)
        self.field1.GetEventHandler().AddPendingEvent(ke)

where self.field1 is a custom wxTextCtrl. I also tried:

        self.field1.GetEventHandler().ProcessEvent(ke),

        self.field1.ProcessEvent(ke),

        self.field1.AddPendingEvent(ke),
and
        ke.Skip(),

but the text control never seems to see this "simulated" key event.
Is there any way to do this, or should I give up on testing derived
controls in this fashion? Thanks.

···

=====
Donnal Walter
Arkansas Children's Hospital

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

I am doing the same thing in my app and it works fine for me. I think the form

self.field1.ProcessEvent(ke)

should work fine, but try:

wxPostEvent(self.field1, ke)

Have you set up a handler for the TextCtrl? i.e. EVT_CHAR(self.field1, self.OnCharFunction)
and implemented a self.OnCharFunction()?

Mark

Donnal Walter wrote:

···

I have been trying to figure out how to simulate an event for
testing purposes. First, I made the following key event class:

class MyKeyEvent(wxKeyEvent):
   def __init__(self, window, keycode):
       wxKeyEvent.__init__(self, wxEVT_CHAR)
       self.m_eventObject = window
       self.m_id = window.GetId()
       self.window = window
       self.m_keyCode = keycode

Then elsewhere I try to simulate a key event as follows:

       ke = MyKeyEvent(self.field1, 49)
       self.field1.GetEventHandler().AddPendingEvent(ke)

where self.field1 is a custom wxTextCtrl. I also tried:

       self.field1.GetEventHandler().ProcessEvent(ke),

       self.field1.ProcessEvent(ke),

       self.field1.AddPendingEvent(ke),
and
       ke.Skip(),

but the text control never seems to see this "simulated" key event.
Is there any way to do this, or should I give up on testing derived
controls in this fashion? Thanks.

=====
Donnal Walter
Arkansas Children's Hospital

Donnal Walter wrote:

I have been trying to figure out how to simulate an event for
testing purposes. First, I made the following key event class:

class MyKeyEvent(wxKeyEvent):
    def __init__(self, window, keycode):
        wxKeyEvent.__init__(self, wxEVT_CHAR)
        self.m_eventObject = window
        self.m_id = window.GetId()
        self.window = window
        self.m_keyCode = keycode

Then elsewhere I try to simulate a key event as follows:

        ke = MyKeyEvent(self.field1, 49)
        self.field1.GetEventHandler().AddPendingEvent(ke)

where self.field1 is a custom wxTextCtrl. I also tried:

        self.field1.GetEventHandler().ProcessEvent(ke),

        self.field1.ProcessEvent(ke),

        self.field1.AddPendingEvent(ke),
and
        ke.Skip(),

but the text control never seems to see this "simulated" key event.
Is there any way to do this,

Yes it should work. I fiddled around in the demo a bit and this works for me:

         ke = wxKeyEvent(wxEVT_CHAR)
         ke.SetEventObject(self.tc1)
         ke.SetId(self.tc1.GetId())
         ke.m_keyCode = ord('A')
         self.tc1.GetEventHandler().ProcessEvent(ke)

or should I give up on testing derived
controls in this fashion? Thanks.

As long as you are aware that this will only cause the event to be delivered to your handler and it won't cause the char to actually be added to the native widget then you should be okay.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!