0

I'm writing a curses application in python.

def main(stdscr):
    stuff(stdscr)

def printStuff(stdscr, string):
    stdscr.clear()
    stdscr.addstr(0, 0, string)
    stdscr.refresh()

def stuff(stdscr):
    printStuff(stdscr, 'foo')
    time.sleep(3)
    printStuff(stdscr, 'bar')
    stdscr.getch()

wrapper(main)

When I run the code, all is good, for the most part. If I press a button in the three second gap at the sleep, the getch will gladly take that as its input, even though the button was pressed before the getch was called. Why is that, and how can i fix this?

Emlingur
  • 163
  • 8
  • 4
    Everything you do is put into an input queue. `getch` processes whatever is in the queue. – Barmar Mar 27 '17 at 19:59

1 Answers1

2

Input is buffered, and getch() processes whatever is in the input buffer. You can clear the buffer with curses.flushinp

def stuff(stdscr):
    printStuff(stdscr, 'foo')
    time.sleep(3)
    printStuff(stdscr, 'bar')
    stdscr.flushinp()
    stdscr.getch()
Barmar
  • 741,623
  • 53
  • 500
  • 612