python - Reset User Prompt After Timeout -
i'm working on issue need execute following scenario:
i prompt user input screen (console window). when prompt displayed basic y/n question, need either a) accept user input response prompt , move on or b) wait preset time , reset prompt previous value, whichever comes first. think of kiosk needs reset if user walks off , you've got idea.
it seems logic (pseudo python below) adds time delay user input after sits there waiting on user reply rather giving on user after time , resetting.
userinput = input("prompt. y/n?") if userinput == "y": move on elif userinput != "y": time.sleep(dwell) reset
if user actively paying attention, i've got works should. but, have feeling hinging on "enter" key being pressed submit input, , don't know how force override input or have watchdog timeout , move along.
also, seems no user input accepted during sleep time. true?
if running on unix use "signal.alarm" function, otherwise use code windows:
import msvcrt import time def raw_input_with_timeout(prompt, timeout=30.0): print prompt, finishat = time.time() + timeout result = [] while true: if msvcrt.kbhit(): result.append(msvcrt.getche()) if result[-1] == '\r': # or \n, whatever win returns;-) return ''.join(result) time.sleep(0.1) # yield other processes/threads else: if time.time() > finishat: return none
instead of returning "none" raise error or whatever want...
Comments
Post a Comment