-1

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

Djazouli
  • 361
  • 1
  • 13

2 Answers2

0

If you use nodelay, that interferes with keypad. Use timeout with a short timeout value, instead. (10 milliseconds for a timeout will work for most people).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Hello, and thanks a lot for this ! I do not know how I missed that in the doc. Unfortunately, my variable does not increment. I do have several windows in my code, do I need to keypad(1) and timeout(10) all of them ? – Djazouli Oct 08 '18 at 11:25
0

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 !

Djazouli
  • 361
  • 1
  • 13