How to input one char at a time from stdin?

Discussion in 'Python' started by Brent W. Hughes, Jan 25, 2005.

  1. I'd like to get a character from stdin, perform some action, get another
    character, etc. If I just use stdin.read(1), it waits until I finish typing
    a whole line before I can get the first character. How do I deal with this?

    Brent
    Brent W. Hughes, Jan 25, 2005
    #1
    1. Advertising

  2. Brent W. Hughes

    Swaroop C H Guest

    On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
    <> wrote:
    > I'd like to get a character from stdin, perform some action, get another
    > character, etc. If I just use stdin.read(1), it waits until I finish typing
    > a whole line before I can get the first character. How do I deal with this?


    This is exactly what you need:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
    Title: "getch()-like unbuffered character reading from stdin on both
    Windows and Unix"

    This recipe was a lifesaver for me once :)

    Regards,
    --
    Swaroop C H
    Blog: http://www.swaroopch.info
    Book: http://www.byteofpython.info
    Swaroop C H, Jan 25, 2005
    #2
    1. Advertising

  3. Swaroop C H wrote:
    > On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
    > <> wrote:
    >
    >>I'd like to get a character from stdin, perform some action, get another
    >>character, etc. If I just use stdin.read(1), it waits until I finish typing
    >>a whole line before I can get the first character. How do I deal with this?

    >
    >
    > This is exactly what you need:
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
    > Title: "getch()-like unbuffered character reading from stdin on both
    > Windows and Unix"
    >
    > This recipe was a lifesaver for me once :)


    Thanks for the link! I've seen this question before a few times, but no
    one had pointed out the recipe.

    Steve
    Steven Bethard, Jan 25, 2005
    #3
  4. Brent W. Hughes

    John Machin Guest

    On Wed, 26 Jan 2005 01:15:10 +0530, Swaroop C H <>
    wrote:

    >On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
    ><> wrote:
    >> I'd like to get a character from stdin, perform some action, get another
    >> character, etc. If I just use stdin.read(1), it waits until I finish typing
    >> a whole line before I can get the first character. How do I deal with this?

    >
    >This is exactly what you need:
    >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
    >Title: "getch()-like unbuffered character reading from stdin on both
    >Windows and Unix"


    Nice to know how, but all those double underscores made my eyes bleed.
    Three classes? What's wrong with something simple like the following
    (not tested on Unix)?


    import sys
    bims = sys.builtin_module_names
    if 'msvcrt' in bims:
    # Windows
    from msvcrt import getch
    elif 'termios' in bims:
    # Unix
    import tty, termios
    def getch():
    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
    else:
    raise NotImplementedError, '... fill in Mac Carbon code here'
    John Machin, Jan 25, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,838
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    768
    Dan Pop
    Jun 21, 2004
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,012
    Ian Collins
    May 9, 2006
  4. Ben
    Replies:
    2
    Views:
    1,327
    jacob navia
    Aug 29, 2009
  5. Terry Cooper
    Replies:
    7
    Views:
    420
    Janos Sebok
    Jun 9, 2009
Loading...

Share This Page