Reading a single character from stdin

by jenny on 10 July 2008 - 05:56pm in

Why there isn't an easier way of doing this, I have no idea. It'd be nice if this would work:

sys.stdin.read(1)

...however, because the file is a tty, nothing gets read until the user hits enter.

Enter getch. It's a utility written by Danny Yoo and stolen from a posting I found on google: ASPN : Python Cookbook : getch()-like unbuffered character reading from stdin on both Windows and Unix.

Description:

A small utility class to read single characters from standard input, on both Windows and UNIX systems. It provides a getch() function-like instance.

Sounds about right, eh? To use it, copy the code below into a file called getch.py and make sure it's on your python search path. Then just do:

from getch import getch
ch = getch()

Getch Utility

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()