Python中的单个按键

Python中的单个按键,python,twisted,Python,Twisted,是一个很好的简单示例,它一次一行地回显来自stdio的输入。我如何才能一次按一个键返回?感谢您提供的指针,添加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 Ec


是一个很好的简单示例,它一次一行地回显来自stdio的输入。我如何才能一次按一个键返回?

感谢您提供的指针,添加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()

可能的副本请谷歌。原来你的问题以前在这里被问过。