Hello. Using the function
EmulateKeyPress(), I want to send a left arrow key (wx.WXK_LBUTTON)
and right arrow key (wx.WXK_LBUTTON) to a text window. I have read the
wxWidgets and wxPython documentation and do not understand how to
create a wx.KeyEvent which is the argument to EmulateKeyPress(). I
have searched the WxPython wiki, Google, and the maillist and I cannot
find an example I understand.
The purpose of sending the keys is to clear the “highlighting” of text
selected in a TextCtrl. This technique, sending the right and then
left button keys to move the caret (insertion point), done manually
will remove the highlighting. Actually, just sending one of the keys
will remove the highlighting, but I want to return the caret to its
original position. I did read the wxWidgets manual and could not
determine any other way to get the desired result.
I am using Windows XP home edition, ActiveState Python build 248 (2.4),
and wxPython 2.6 Unicode build. Below is test code I am using to
experiment with to try and get the syntax correct. I have been able
to consistently cause my application to freeze :-).
Thank you,
Ira
`import wx
---------------------------------- GUI
···
class Frame(wx.Frame):
def __init__(self, parent = None, id = -1, title='Title',
pos = wx.DefaultPosition, size = (600, 400)):
wx.Frame.__init__(self, parent, id, title, pos, size)
self.MyPanel = wx.Panel(self, -1, pos = wx.DefaultPosition,
size = wx.DefaultSize)
text = "The quick brown fox jumped over the lazy grey
programmer.\nTesting: 1, 2, 3, …"
self.MyTextCtrl = wx.TextCtrl(parent = self.MyPanel, id = -1,
value = text,
pos = (100, 50), size = (400,
300),
style = wx.VSCROLL | wx.TE_RICH2
wx.TE_NOHIDESEL |
wx.TE_MULTILINE | wx.TE_READONLY)
self.MyTextCtrl.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)
self.MyTextCtrl.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
def OnMouseLeftUp(self, evt):
# did the user select any text?
if self.TextSelected(evt):
self.DisplayMarkers()
evt.Skip()
def OnKeyUp(self, evt):
if self.TextSelected(evt):
self.DisplayMarkers()
evt.Skip()
def TextSelected(self, evt):
" return True if the user has highlighted/selected text in
TextCtrl “”"
from_position, to_position = self.MyTextCtrl.GetSelection()
if from_position != to_position:
# cancel the selection by removing the highlighting: move
the caret left, then right
wx.KeyEvent.m_keyCode = wx.WXK_LBUTTON
self.MyTextCtrl.EmulateKeyPress(evt.Clone(wx.KeyEvent.m_keyCode)))
wx.KeyEvent.m_keyCode = wx.WXK_RBUTTON
self.MyTextCtrl.EmulateKeyPress(evt.Clone(wx.KeyEvent.m_keyCode))
return True
else:
return False
def DisplayMarkers(self):
""" display bitmaps to denote beginning and end of selected
text range “”"
# to be written
print "DisplayMarkers() callled"
------------------------------- application
class App(wx.App):
def OnInit(self):
self.title = "Test"
self.frame = Frame(title = self.title)
self.frame.Show()
self.SetTopWindow(self.frame)
return True
---------------------------------- main
def main():
app = App()
app.MainLoop()
if name == ‘main’:
main()
`