I am trying to unit test a GUI, and as part of that I need to
programmatically send keys to various widgets (ex: list, text field,
etc...).
I wrote the following class:
class CanBeSentKeys:
def __init__(self):
pass
def SendKey(self, key_code):
key_event = wxKeyEvent(wxEVT_CHAR)
key_event.m_keycode = key_code
print "\n-- SendKey: key_code=%s, key_event.GetKeyCode()=%s" %
(key_code, key_event.GetKeyCode())
self.ProcessEvent(key_event)
This class is meant to be mixed in with any widget that can process a
wxKeyEvent like this:
class wxListCtrlWithHelpers(wxListCtrl, CanBeSentKeys):
"""A wxListCtrl subclass with helpers for finding more about the data
that is displayed in it, and programmatically simulating user actions on
it.
"""
def __init__(self, frame, id, pos=wxDefaultPosition, size=wxDefaultSize,
style=wxLC_REPORT | wxLC_HRULES | wxLC_SINGLE_SEL):
wxListCtrl.__init__(self, frame, id, pos, size, style)
CanBeSentKeys.__init__(self)
Etc...
This sort of works. If I create a wxListCtrlWithHelpers, say some_list, then
invoke some_list.SendKey(WXK_DOWN), then the EVT_CHAR handler associated
with some_list does get invoked. Unfortunately, if I do GetKeyCode() on the
event it received, it says 0. In fact, the SendKey() method also says that
the event it creates has GetKeyCode() = 0.
So my questions are:
1) is there a better way of doing this with wxPython?
2) if not, how can I set the key code for the event generated by SendKey()
Thx.
Alain Désilets, MASc
Agent de recherches/Research Officer
Institut de technologie de l'information du CNRC /
NRC Institute for Information Technology
alain.desilets@nrc-cnrc.gc.ca
Tél/Tel (613) 990-2813
Facsimile/télécopieur: (613) 952-7151
Conseil national de recherches Canada, M50, 1200 chemin Montréal,
Ottawa (Ontario) K1A 0R6
National Research Council Canada, M50, 1200 Montreal Rd., Ottawa, ON
K1A 0R6
Gouvernement du Canada | Government of Canada