本文整理汇总了Python中msvcrt.getche方法的典型用法代码示例。如果您正苦于以下问题:Python msvcrt.getche方法的具体用法?Python msvcrt.getche怎么用?Python msvcrt.getche使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msvcrt
的用法示例。
在下文中一共展示了msvcrt.getche方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: grab
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import getche [as 别名]
def grab(self, prompt, **kwargs):
# Takes a prompt, a default, and a timeout and shows it with that timeout
# returning the result
timeout = kwargs.get("timeout", 0)
default = kwargs.get("default", None)
# If we don't have a timeout - then skip the timed sections
if timeout <= 0:
if sys.version_info >= (3, 0):
return input(prompt)
else:
return str(raw_input(prompt))
# Write our prompt
sys.stdout.write(prompt)
sys.stdout.flush()
if os.name == "nt":
start_time = time.time()
i = ''
while True:
if msvcrt.kbhit():
c = msvcrt.getche()
if ord(c) == 13: # enter_key
break
elif ord(c) >= 32: #space_char
i += c
if len(i) == 0 and (time.time() - start_time) > timeout:
break
else:
i, o, e = select.select( [sys.stdin], [], [], timeout )
if i:
i = sys.stdin.readline().strip()
print('') # needed to move to next line
if len(i) > 0:
return i
else:
return default
示例2: _wait_for_exit_on_windows
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import getche [as 别名]
def _wait_for_exit_on_windows(please_stop):
import msvcrt
line = ""
while not please_stop:
if msvcrt.kbhit():
chr = msvcrt.getche()
if ord(chr) == 13:
if line == "exit":
Log.alert("'exit' Detected! Stopping...")
return
elif ord(chr) > 32:
line += chr
else:
sleep(1)
示例3: input_with_timeout
# 需要导入模块: import msvcrt [as 别名]
# 或者: from msvcrt import getche [as 别名]
def input_with_timeout(timeout):
if platform.system() == 'Windows':
start_time = time()
s = ''
while True:
while msvcrt.kbhit():
c = msvcrt.getche()
if ord(c) == 13: # enter_key
break
elif ord(c) >= 32: #space_char
s += c.decode('utf-8')
if time() - start_time > timeout:
return None
return s
else:
while True:
try:
rlist, _, _ = select.select([sys.stdin], [], [], timeout)
break
except (OSError, select.error) as e:
if e.args[0] != errno.EINTR:
raise e
if rlist:
return sys.stdin.readline()
else:
return None