Search code examples
pythonpython-3.xlistenercurses

Can not get a key with curses


I am trying to use curses to display some statistics, and I do face a problem.

I wanted to have a window which allow scrolling thanks to the keyboard. For this, I create a variable self.scroll, which tells me which lines I should display. The problem is that I want to increment this variable whenever I press KEY_DOWN.

Here is my code : In the init of the class, I do have :

    self.stdscr = stdscr
    self.scroll = 0
    stdscr.nodelay(1)
    stdscr.keypad(1)

Then :

    while True:
        ch = self.stdscr.getch()
        if ch == curses.KEY_DOWN:
            self.scroll += 1
            self.add_alert()
            ch = None
        elif ch == curses.KEY_UP:
            if self.scroll >= 1:
                self.scroll -= 1
            self.add_alert()
            ch = None

I also used a wrapper that can be found here to initialize everything.

The fact is that the variable scroll is stuck at 0, no matter what. Moreover, I see every key that I press (e.g ^[[A) whenever I press it, even if noecho() is set. I used nodelay(), because my thread is also processing some things, and I do not want it to be stopped while waiting for a key to be pressed. Do you have any idea from where it could come ?

Thanks a lot, Djaz


Solution

  • Finally, it was working from the beginning. The problem was just that for some strange reasons, curses did not detect KEY_UP and KEY_DOWN. I just replaced them by u and d

    if ch == ord('p'):
    

    and everything is working fine.

    Thanks to everyone !