我正在尝试从stdin中进行非阻塞地读取单个字符。我已经找到了一个使用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
一切正常,除了缩进输出出现了问题。有没有办法解决这个问题?