How to input one char at a time from stdin?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brent W. Hughes

    How to input one char at a time from stdin?

    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


  • Swaroop C H

    #2
    Re: How to input one char at a time from stdin?

    On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
    <brent.hughes@c omcast.net> wrote:[color=blue]
    > 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?[/color]

    This is exactly what you need:

    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

    Comment

    • Steven Bethard

      #3
      Re: How to input one char at a time from stdin?

      Swaroop C H wrote:[color=blue]
      > On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
      > <brent.hughes@c omcast.net> wrote:
      >[color=green]
      >>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?[/color]
      >
      >
      > This is exactly what you need:
      > http://aspn.activestate.com/ASPN/Coo.../Recipe/134892
      > Title: "getch()-like unbuffered character reading from stdin on both
      > Windows and Unix"
      >
      > This recipe was a lifesaver for me once :-)[/color]

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

      Steve

      Comment

      • John Machin

        #4
        Re: How to input one char at a time from stdin?

        On Wed, 26 Jan 2005 01:15:10 +0530, Swaroop C H <swaroopch@gmai l.com>
        wrote:
        [color=blue]
        >On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
        ><brent.hughes@ comcast.net> wrote:[color=green]
        >> 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?[/color]
        >
        >This is exactly what you need:
        >http://aspn.activestate.com/ASPN/Coo.../Recipe/134892
        >Title: "getch()-like unbuffered character reading from stdin on both
        >Windows and Unix"[/color]

        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_mod ule_names
        if 'msvcrt' in bims:
        # Windows
        from msvcrt import getch
        elif 'termios' in bims:
        # Unix
        import tty, termios
        def getch():
        fd = sys.stdin.filen o()
        old_settings = termios.tcgetat tr(fd)
        try:
        tty.setraw(sys. stdin.fileno())
        ch = sys.stdin.read( 1)
        finally:
        termios.tcsetat tr(fd, termios.TCSADRA IN, old_settings)
        return ch
        else:
        raise NotImplementedE rror, '... fill in Mac Carbon code here'

        Comment

        Working...