python 獲取鍵盤輸入,同時有超時的功能示例
更新時間:2018年11月13日 10:25:08 作者:madrabbit1987
今天小編就為大家分享一篇python 獲取鍵盤輸入,同時有超時的功能示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
''' ###get keyboard input and timeout =5 import sys, time, msvcrt def readInput( caption, default, timeout = 5): start_time = time.time() sys.stdout.write('%s(%s):'%(caption, default)); input = '' while True: if msvcrt.kbhit(): chr = msvcrt.getche() if ord(chr) == 13: # enter_key break elif ord(chr) >= 32: #space_char input += chr if len(input) == 0 and (time.time() - start_time) > timeout: break print '' # needed to move to next line if len(input) > 0: return input else: return default readInput("TEst1",10) ''' ###catch keyboard input, if key == ESC, stop import sys, time, msvcrt def readKeyBoardInput(timeout = 5): start_time = time.time() sys.stdout.write("If you want to stop test process,please click ESC button"); input = '' while True: if msvcrt.kbhit(): chr = msvcrt.getche() if ord(chr) == 27: # ESC return True if len(input) == 0 and (time.time() - start_time) > timeout: return False
以上這篇python 獲取鍵盤輸入,同時有超時的功能示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python爬取百度地圖POI數(shù)據(jù)代碼的步驟
爬取百度地圖的POI數(shù)據(jù)涉及法律和道德問題,因為這類數(shù)據(jù)受到版權(quán)保護(hù),且大多數(shù)在線地圖服務(wù)都有嚴(yán)格的反爬蟲措施,這篇文章主要介紹了Python爬取百度地圖POI數(shù)據(jù)代碼,需要的朋友可以參考下2024-08-08解決Django響應(yīng)JsonResponse返回json格式數(shù)據(jù)報錯問題
這篇文章主要介紹了解決Django響應(yīng)JsonResponse返回json格式數(shù)據(jù)報錯問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Python開發(fā)的單詞頻率統(tǒng)計工具wordsworth使用方法
wordsworth是字母,單詞和n元組頻率分析,用來分析文件中的單詞出現(xiàn)頻率的工具。2014-06-06