Reading a keypress

wyleu chris.lyon at spritenote.co.uk
Mon Feb 25 13:35:54 EST 2008


I'm trying to read a single keypress on Linux but expect to have the
programme running on Windows platform as well and find the mention in
the FAQ:

import termios, fcntl, sys, os
fd = sys.stdin.fileno()

oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)

oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

try:
    while 1:
        try:
            c = sys.stdin.read(1)
            print "Got character", `c`
        except IOError: pass
finally:
    termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)

However this fails on the second line as sys.stdin seems to have no
method fileno.
Any idea how I might proceed?



More information about the Python-list mailing list