Sign in to follow this  
BerwynIrish

Python - clearing events?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this