pythonwxpythonscintilla

Capturing enter key for WxPython Styled Text Control


Using a StyledTextControl in wxpython by importing wx.stc, how can i capture the Enter Key as an event? i've tried several methods but all seem to be saying errors or not working at all.

so fair ive got:

self.text.Bind(wx.stc.STC_KEY_DOWN, self.hw)

def hw(self, event):
  if event.GetKeyCode == wx.stc.STC_KEY_RETURN:
    self.text.AddText('Hello World')
    event.Skip()

where self.text is my stc.

Just doesnt seem to be working for me. Do i use wx functions or stc functions for capturing the event?


Solution

  • I believe that you should use StyledTextCtrl events for handling events in a StyledTextCtrl, not wx events.

    The most obvious event to handle would seem to be EVT_STC_KEY. However, according to the documentation for the event, this might not work:

    The Scintilla documentation says:

    Reports all keys pressed. Used on GTK+ because of some problems with keyboard focus. Not sent by Windows version.

    As far as this writer can tell, this means that this event, although technically available thru wxWindows, will not actually ever occur.

    You can try handling the EVT_STC_CHARADDED event instead, but that seems to be called after a new line has been added. Calling event.Skip() for this event has no effect. On Windows, this event fires twice, once for the carriage-return \r and once for the newline \n.

    Ultimately, it's possible that what you are trying to achieve might not be possible.

    (Incidentally, STC_KEY_DOWN is a code that represents the down-arrow key, not that a key has been pressed down.)