2012年7月16日星期一

My solution for shortcuts "ctrl+a" in wx.TextCtrl

I want to type in "ctrl+a" combination to select all the content in  the wx.TextCtrl of my application, but I find that is not supported defaultly as "ctrl+z"(repeal) and other shortcuts. I search for the solution on net, while most are for applications with menu or a "select all" button. However, there is no menu or a specific button in my app. At last I find a solution in wx.KeyEvent document, and the code below is my solution:


          # imgEdit is the textctrl in my app
          self.imgEdit.Bind(wx.EVT_CHAR, self.OnSelectAll)
       def OnSelectAll(self, evt):          keyInput = evt.GetKeyCode()
          if keyInput == 1:  # 1 stands for 'ctrl+a'
              self.imgEdit.SelectAll()
              pass
          evt.Skip()


     We must use wx.EVT_CHAR here instead of wx.EVT_KEY_DOWN or wx.EVT_KEY_UP, for wx.EVT_CHAR can translate the combination of "ctrl" and "a" into ASCIKII "1".At the end of the response function we must keep in mind to type in "evt.Skip()", or the textctrl will not be typed in anything.

没有评论:

发表评论