Reading a keypress

wyleu chris.lyon at spritenote.co.uk
Mon Feb 25 15:52:53 EST 2008


>
> A recipe that supposedly does this in a cross-platform way:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892

class _Getch:
    """Gets a single character from standard input.  Does not echo to
the
screen."""
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()


class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch


class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()


getch = _Getch()



Sadly this also fails with:


Traceback (most recent call last):
  File "<pyshell#2>", line 1, in -toplevel-
    a = getch()
  File "/home/chris/getch.py", line 10, in __call__
    def __call__(self): return self.impl()
  File "/home/chris/getch.py", line 19, in __call__
    fd = sys.stdin.fileno()
AttributeError: fileno


What is fileno, and why might I not  have it?










More information about the Python-list mailing list