By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

How to input one char at a time from stdin?

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
<br**********@comcast.net> 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/Coo.../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
Jul 18 '05 #2

P: n/a
Swaroop C H wrote:
On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
<br**********@comcast.net> 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/Coo.../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
Jul 18 '05 #3

P: n/a
On Wed, 26 Jan 2005 01:15:10 +0530, Swaroop C H <sw*******@gmail.com>
wrote:
On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
<br**********@comcast.net> 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/Coo.../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'

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.