Question

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?

Was it helpful?

Solution 3

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.)

OTHER TIPS

Hi I know this is a little old now but I used this and it seems to work.

import wx
import wx.stc

self.m_message = TextCtrl( self, ID_ANY, EmptyString, DefaultPosition, Size( 200,100 ), 0 )
self.m_message.Bind(EVT_KEY_DOWN, self.DoKeyPress)

def DoKeyPress(self, event):
    if event.GetKeyCode() == STC_KEY_RETURN:
        self.DoSendTextMessage(event)
    else:
        event.Skip()

This is actually explained in the documentation: https://wxpython.org/Phoenix/docs/html/wx.TextCtrl.html. Setting the style to TE_PROCESS_ENTER will enable the event and then you can just bind it.

from wx import TextCtrl, EVT_TEXT_ENTER, TE_PROCESS_ENTER
text_input = TextCtrl(parent, style=TE_PROCESS_ENTER)
text_input.Bind(EVT_TEXT_ENTER, callback)

You may use wx.EVT_KEY_DOWN, and Return key is 13

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top