clitools.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:skaff 作者: jhxie 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号