简体   繁体   English

Python Twisted中的单个按键

[英]Individual key presses in Python Twisted

https://twistedmatrix.com/documents/current/_downloads/stdin.py https://twistedmatrix.com/documents/current/_downloads/stdin.py

is a nice simple example which echos back line-at-a-time the input from stdio. 这是一个很好的简单示例,它一次回显stdio的输入。 How can I get back individual key presses one at a time? 如何一次取回单个按键?

Thank you for the pointers, adding in the tty/termios and using setRawMode() worked for me: 感谢您提供的指针,添加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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM