Search code examples
pythoncurses

Python curses - hide user input


I am developing one app in python curses. I am using getch() method to get pressed keys. But I can see pressed keys on screen. I can move cursor whenever I want, but after cursor I can see what user typed.

Of course, I can erase and redraw the whole screen after every pressed key, but it's blinking - that's distrubing.

Any idea how to get rid of these pressed keys? Thank you


Solution

  • Initialize the curses class in the following way, it will solve the problem.

    class curses_screen:
        def __enter__(self):
            self.stdscr = curses.initscr()
            curses.cbreak()
            curses.noecho()
            self.stdscr.keypad(1)
            SCREEN_HEIGHT, SCREEN_WIDTH = self.stdscr.getmaxyx()
            return self.stdscr
        def __exit__(self,a,b,c):
            curses.nocbreak()
            self.stdscr.keypad(0)
            curses.echo()
            curses.endwin()
    
    with curses_screen() as stdscr:
        """
        Execution code plush getch code here
        """