View Full Version : How to flush input buffer with Python/Curses/Getch?

September 10th, 2007, 07:06 PM
I'm messing around with Curses programming, but I can't figure out how to clear the buffer when using getch to capture keystrokes.

Basically, when you hold a key down it remembers multiple keystrokes, and I need to make sure it only reads the last key pressed.

sys.stdout.flush() doesn't seem to work...

September 11th, 2007, 01:21 AM

September 13th, 2007, 01:12 AM

September 13th, 2007, 01:35 AM
looks like no deep-level command-level Python hackers around - only web apps weenies like me :-)

You may have more luck at pytho.org mailing list - good luck!

Lux Perpetua
September 13th, 2007, 05:53 AM
I don't know about python/curses, but I wrote a C program with curses a while ago that included this. It simply read characters (after calling nodelay) until a read resulted in ERR. I don't know how much it would help you, but from what I understand, curses in python simply wraps the C library. If you want, I can post the C program. It's only 46 lines.

September 13th, 2007, 06:02 AM
we did it the same way, the other way is to turn off buffering for input (meaning you get a key at a time, not a line), this way you can manage the input buffer yourself :)

but technically, IMO there is no such thing as "flushing" then input buffer, since you are not the one who is putting things in there ...

the user flushes the buffer (usually by pressing the Enter key)

July 7th, 2009, 10:01 PM
Sorry to respond to an old thread... but I wanted people who got here from google to have an answer.

curses.flushinp() apparently does what I need it to.

Flush all input buffers. This throws away any typeahead that has been typed by the user and has not yet been processed by the program.