This is my attempt:
from inputimeout import inputimeout, TimeoutOccurred
import time
import sys
import os
def get_choice(maxtimeout=9):
try:
for remaining in range(maxtimeout, 0, -1):
sys.stdout.write("\r")
sys.stdout.write(
"Want to continue to next item(Y/N)? Defaults to Y in {:2d} seconds...".format(
remaining + 1
)
)
sys.stdout.flush()
time.sleep(1)
c = inputimeout(prompt="", timeout=1)
except TimeoutOccurred:
c = "Y"
return c
The issues with my approach are:
- The inputed key is not visible until the timeout completes.
- The user has to press enter after his choice.
- The user has to wait till timeout completes irrespective of when he entered the value.
signal
module to set a timer to go off if the user hasn't provided input within the given amount of time. I can provide an example later if you're unable to get it working.