失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python 获取键盘输入 同时有超时的功能_python 获取键盘输入 同时有超时的功能示例...

python 获取键盘输入 同时有超时的功能_python 获取键盘输入 同时有超时的功能示例...

时间:2023-09-08 08:12:07

相关推荐

python 获取键盘输入 同时有超时的功能_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 获取键盘输入,同时有超时的功能示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

如果觉得《python 获取键盘输入 同时有超时的功能_python 获取键盘输入 同时有超时的功能示例...》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。