我正在尝试对标准输入中的单个字符进行非阻塞读取。我找到了curses库的解决方案,但是在尝试将输出写回stdout时我做错了。
import curses
from time import sleep
def callback(screen):
screen.nodelay(1)
return screen.getkey()
while 1:
try:
key = curses.wrapper(callback)
print "Got keypress: ", key
except:
sleep(3)
print "No Keypress"
print "Program\nOutput"
# Prints
No Keypress
Program
Output
除了缩进输出之外,一切都完美无缺。有没有什么办法解决这一问题?