def timed_key_get(seconds: int) -> str:
"""
Gets a single key press from the terminal within the given number of
'seconds' and returns a 'str' representing the key pressed; returns empty
"" string upon time out.
Based on the version from StackOverflow by Paul:
http://stackoverflow.com/questions/3471461/raw-input-and-timeout/3911560
"""
if "posix" == os.name:
return timeout(seconds)(key_get)()
elif "nt" == os.name:
start_time = time.time()
key = str()
while True:
if msvcrt.kbhit():
key = key_get()
break
if len(key) == 0 and (time.time() - start_time) > seconds:
raise TimeoutError()
return key
评论列表
文章目录