Search code examples
python-3.xcursespython-cursestui

Custom key-bind functionality in curses


I'm trying to implement some simple text editing functionality in a TUI I'm working on. I'm using the Python wrapper for curses.

Currently what I'm trying to do is associate certain key-binds to a particular function or operation, but unsure of how to do it and the documentation isn't clear. According to the documentation there are some default keybinds, but what if I wanted to add my own "event" associated with a particular keybind?


Solution

  • The place to start is by looking at the source code of curses.textpad to see how it is implemented. It's hardcoded, e.g.,

        elif ch == curses.ascii.ENQ:                           # ^e
            if self.stripspaces:
                self.win.move(y, self._end_of_line(y))
            else:
                self.win.move(y, self.maxx)
    

    It could be rewritten to allow for custom bindings, but keep in mind that at the moment (mid-February 2021), the most recent commit was more than 4 years ago (December 2016).