[Python] catch TextCtrl's key/char events and forward typed key to the stdin of a subprocess

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to use wx.TextCtrl to catch the typed key events, and directly forward the typed key to the stdin of a subprocess. Please note, for my special purpose, I will completely disable the text editing feature of the TextCtrl. i.e., when I type a letter, the letter will not be appearing on the TextCtrl, it will be directly forwarded.
    Here is some code to illustrate what I want.

    # inside the main frame
    self.text = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_MULTILINE)
    self.text.Bind(wx.EVT_KEY_DOWN, self.OnKey)
    self.text.Bind(wx.EVT_CHAR, self.OnChar)
    # ...

    def OnKey(self, evt):
    keycode = evt.GetKeyCode()
    # ENTER

    if keycode == 13:
    self.subprocess.stdin.read("\n")
    if keycode == 9:
    self.subprocess.stdin.read("\t")
    if keycode == 8:
    self.subprocess.stdin.read("\b")
    if keycode == 316:
    pass # maybe some key will be ignored
    else:
    evt.skip()

    def OnChar(self, evt):
    key=chr(keycode)
    self.subprocess.stdin.read(key)


    I want to forward "ENTER", "TAB", "BACKSPACE", characters, numbers, etc., all the key input events to stdin, without letting TextCtrl to interfere. Is there a good way to do it? Or I have to explicitely match each key one by one?

    Thanks for any suggestions!

    Continue reading...

Compartilhe esta Página