Python - clearing events?

Started by
0 comments, last by FluxCapacitor 17 years, 6 months ago
I've got a program in Python that binds keypress events to a function. Inside this function, if the key pressed is a certain key, then I use os.system() to run another program. This program can take up to 15 seconds before the user sees anything different on the screen, during which time the user might assume that the keypress didn't register and so hit the key again. If this happens, then when the program called from os.system() exits, my program sees the second keypress and passes it along to my binded method, resulting in another os.system() call. Is there a command to clear keypresses or something similar which I can use to prevent os.system() from executing a second time? I've tried setting a flag as the first thing in the block for this key which will cause my binded method to exit right away if the flag is set. I've also tried using the unbind method to disable my key processing method and then rebinding when the called program terminates. Niether of these work.
Advertisement
There should be a way to flush the input buffer; you might try sys.stdin.flush(), but from the little poking around I did on google it looks like that doesn't actually do anything. You might also try storing keypresses in a list when they are received rather than sending them directly to your function; then in your main loop you can iterate through your list and send each keypress value in the list to the appropriate function. After returning from the other program you would just clear the input list to get rid of the unwanted keypresses.

This topic is closed to new replies.

Advertisement