https://twistedmatrix.com/documents/current/_downloads/stdin.py
是一个很好的简单示例,它回显来自stdio的输入。如何一次取回一个按键?
答案 0 :(得分:1)
感谢您的指示,添加tty / termios并使用setRawMode()为我工作:
#!/usr/bin/python
import sys, tty, termios
from twisted.internet import stdio
from twisted.protocols import basic
from twisted.internet import reactor
class Echo(basic.LineReceiver):
def connectionMade(self):
self.setRawMode()
def rawDataReceived(self, line):
for c in line:
self.sendLine("[%02x]" % ord(c))
if ord(c) == 3:
reactor.stop()
def main():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(sys.stdin.fileno())
stdio.StandardIO(Echo())
reactor.run()
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
if __name__ == '__main__':
main()