简体   繁体   中英

Individual key presses in Python Twisted

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

is a nice simple example which echos back line-at-a-time the input from 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:

#!/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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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