python 获取键盘输入,同时有超时的功能示例
如下所示:
''' ###getkeyboardinputandtimeout=5 importsys,time,msvcrt defreadInput(caption,default,timeout=5): start_time=time.time() sys.stdout.write('%s(%s):'%(caption,default)); input='' whileTrue: ifmsvcrt.kbhit(): chr=msvcrt.getche() iford(chr)==13:#enter_key break eliford(chr)>=32:#space_char input+=chr iflen(input)==0and(time.time()-start_time)>timeout: break print''#neededtomovetonextline iflen(input)>0: returninput else: returndefault readInput("TEst1",10) ''' ###catchkeyboardinput,ifkey==ESC,stop importsys,time,msvcrt defreadKeyBoardInput(timeout=5): start_time=time.time() sys.stdout.write("Ifyouwanttostoptestprocess,pleaseclickESCbutton"); input='' whileTrue: ifmsvcrt.kbhit(): chr=msvcrt.getche() iford(chr)==27:#ESC returnTrue iflen(input)==0and(time.time()-start_time)>timeout: returnFalse
以上这篇python获取键盘输入,同时有超时的功能示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。