473,594 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 18 '05 #1
3 15946
On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
<br**********@c omcast.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
Swaroop C H wrote:
On Tue, 25 Jan 2005 12:38:13 -0700, Brent W. Hughes
<br**********@c omcast.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
On Wed, 26 Jan 2005 01:15:10 +0530, Swaroop C H <sw*******@gmai l.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_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'

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4371
by: User | last post by:
Anyone know of a function like raw_input(), but with a built-in or user-specified time limit? Ideally, it would return an empty string ('') and resume processing if the user didn't enter something within ~10 seconds.
6
2319
by: Florian Wilhelm | last post by:
I have some easy questions, but I cant figure them out: I want to read exactly one char from stdin, there should be no need to hit enter. import sys print "Input: (y/N) ", input = sys.stdin.read(1) print "Your input:", input
2
1989
by: SAN CAZIANO | last post by:
how can I have on the right of an input field the number of the char that we can put on it (when it's length is changed this number must changed itself: perhaps onkeypress event)???
1
1832
by: Sunil Korah | last post by:
Hi, I have a form with just 2 text boxes to get a start date and end date to pass to a query. i have set the format to short date (which is set to "mm/dd/yy" in the regional settings of windows). The default values in both are now(). When the form comes up the dates are displayed correctly. But when the user clicks in the text box it displays the long date format including the time part. How do I make sure that this does not happen?
6
2473
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to confirm whether he really wants to overwrite the existing output file. The problem is that the second read from stdin, to obtain the user response whether to overwrite the existing output file, never waits for the user's response. It's as if a...
2
6008
by: ShawnD | last post by:
I'm having some issues when trying to read input off of a pipe using a python script. I'm trying to process packet data from tcpdump in real-time, so it's a filter that needs to read data while the process being piped from is still reading. On both my Linux and OSX system, it seems that there's a large delay reading from stdin if the process being read from is still running, even if it's producing output to be read. It will wait until it...
7
2001
by: dtschoepe | last post by:
Hi, I am working on a project for school and I am trying to get my head around something weird. I have a function setup which is used to get an input from stdin and a piece of memory is created on the heap using malloc. This input is then run through a regex and another test. The result is passed back as a char pointer to the program for further processing The first time I call this function, it performs normally. However,
5
14454
by: j.smith2c | last post by:
Hi everyone, I wanted to run the following loop, and enter y everytime (I expected that it will ask me to enter y for 5 time), but just after 3 times program terminates. what is the error?? #include <stdio.h>
12
2076
by: Tarique | last post by:
I have tried to restrict the no. of columns in a line oriented user input.Can anyone please point out potential flaws in this method? btw.. 1.I have not used dynamic memory allocation because thats not something i want to implement. 2.I suppose my input line oriented . 3.The maximum columns i need is 80 4.There is an infinite loop in the program :) 5.I have *not* tried to validate the data as yet.
0
7876
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8003
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8234
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6654
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5408
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2385
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.