睡眠时忽略python中的键盘输入 [英] Ignore keyboard input in python while sleeping
问题描述
我有一个带有循环的代码,要求输入一些内容,然后睡眠一段时间.目的是防止人为错误,迫使用户在输入下一个输入之前要等待. 问题是,python在time.sleep时不会忽略键盘输入.
I'm having a code with a loop asking for some input and then sleeps for some time. The purpose is to prevent for human errors, forcing the user to wait before entering the next input. The problem is, that python does not ignore the keyboard inputs while time.sleep.
这是示例代码:
from time import sleep
from msvcrt import getch
for i in range(3):
print 'Enter a character'
char = getch()
sleep(2)
print char
在time.sleep()中连续按下某些字符时,msvcrt.getch()会记住它们,并且循环运行到最后.我希望在睡眠期间按下的每个字符都将被忽略.我该怎么办?
When pressing some characters consecutively during time.sleep(), msvcrt.getch() remembers them and the loop runs to the end. I want that every character pressed during sleep will be ignored. How can I do that?
推荐答案
对不起,我之前发布过错误的答案(尽管已删除),这是您要解决的人为问题.
Sorry for wrong answer that i have posted before(as i have deleted though), it is the human level problem that you are asking for.
程序线程是唯一进入睡眠状态的事件,而键盘进程显然仍在运行时,它将记录所有输入并从睡眠状态恢复后进行输入.
无法以这种方式实现
这篇关于睡眠时忽略python中的键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!