473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alternative to raw_input ?

I need something like "Press any key to continue" code for my program.
Currently I use : raw_input("Pres s Enter to continue ") but it's lame.
Jul 18 '05
16 5648
On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:
print prompt
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()


Thanks, it works but without line "print prompt" and also
I'm not sure if I should put this function :

def cekaj():
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()

#Or this one, which sounds more logical according to help
#kbhit() - Return true if a keypress is waiting to be read.

def cekaj():
msvcrt.getch()
while msvcrt.kbhit():
msvcrt.getch()

It works both ways, not sure which one is right
Jul 18 '05 #11
BOOGIEMAN wrote:
On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:

print prompt
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch( )

Thanks, it works but without line "print prompt" and also
I'm not sure if I should put this function :

def cekaj():
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()

#Or this one, which sounds more logical according to help
#kbhit() - Return true if a keypress is waiting to be read.

def cekaj():
msvcrt.getch()
while msvcrt.kbhit():
msvcrt.getch()

It works both ways, not sure which one is right


Try this:

print "Hit a key!"
cekaj()
print "Nap time!"
time.sleep(15)
print "Hit another key!"
cekaj()

with the two different implementations , and see what happens if you hit a key
when the 'Nap Time!' prompt appears.

Cheers,
Nick.

P.S. You probably actually want an implementation that looks like:

def cekaj(prompt="P ress a key to continue"):
while msvcrt.kbhit():
msvcrt.getch()
if prompt is not None:
print prompt
msvcrt.getch()

And the sample program would look like:
cekaj("Hit a key!")
print "Nap time!"
time.sleep(15)
cekaj("Hit another key!")

--
Nick Coghlan | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #12
BOOGIEMAN wrote:
On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:
print prompt
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch( )

Thanks, it works but without line "print prompt" and also


That was intended to be a hint that you might need
to print a prompt to the user (maybe a string contained
in a variable named "prompt", for example ;-), or the
program might halt without apparent reason.
I'm not sure if I should put this function :

def cekaj():
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()

#Or this one, which sounds more logical according to help
#kbhit() - Return true if a keypress is waiting to be read.

def cekaj():
msvcrt.getch()
while msvcrt.kbhit():
msvcrt.getch()

It works both ways, not sure which one is right


The point of doing the first approach is that you are
reading *all* available keystrokes (in the loop), and
then sitting and waiting for one more keystroke before
continuing. If you don't read all keystrokes first,
then if the user has hit a key, say, five minutes
earlier, it will still be sitting in the queue and
your program will not actually stop at all.

Your second approach sits and waits (if necessary)
to read a single keystroke, and then if the user had
managed to hit two or more keys** before that, it will
consume all remaining keystrokes before continuing.
Not exactly the same thing, nor generally quite
what you want. Based on what you originally asked for,
the former is the correct approach.

** Note that some keys will result in a pair of
values being retrieved by getch(), with the first one
being (generally... maybe always... I can't remember
but it's easy for you to experiment) a 0, and the second
being another code that identifies the key. The F5
key, for example, returns a 0 and then a 64 on the
second call to getch(). If you *really* want *any*
key to continue, you'll want to check for this sort
of thing as well... though personally I'd just use
raw_input() and keep my program portable. <wink>

-Peter
Jul 18 '05 #13
On Sun, 13 Feb 2005 12:08:26 +1000, Nick Coghlan wrote:
Try this:

print "Hit a key!"
cekaj()
print "Nap time!"
time.sleep(15)
print "Hit another key!"
cekaj()

with the two different implementations , and see what happens if you hit a key
when the 'Nap Time!' prompt appears.


I see the difference now, thanks
Jul 18 '05 #14
Le vendredi 11 Février 2005 18:00, den a écrit*:
import msvcrt
msvcrt.getch()


I frequently had the problem to have something similar but *portable*.
Something as short and simple.

Someone have an idea ?

Thank you

Francis Girard

Jul 18 '05 #15
Then may I suggest the keeping-it-simple approach:

def myGetch():
.....raw_input( "Press Enter to continue")
(sorry about the dots, I'm using google groups)

Lars

Jul 18 '05 #16
Francis Girard wrote:
[den]:
import msvcrt
msvcrt.getch( )


I frequently had the problem to have something similar but *portable*.
Something as short and simple.


This has been brought up many times on this newsgroup. The answer is that
there is no simple way to do this portably. You could use Python's
exception handling to create a version that runs on multiple systems
though, falling back to another method if msvcrt can't be imported.
--
Michael Hoffman
Jul 18 '05 #17

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

Similar topics

1
2521
by: Hugh | last post by:
I am using python 2.3 through the PythonWin program on windows. I would like to create a console-based interactive session. The program raw_input is almost exactly what I'd like, except that whenever I call raw_input(), it pops up a window on my screen. I'd much rather have it read from the interactive window. Is there something out there, as easy as raw_input(), that I can use? Thanks for any clues!
5
3001
by: Helmut Jarausch | last post by:
Hi when using an interactive Python script, I'd like the prompt given by raw_input to go to stderr since stdout is redirected to a file. How can I change this (and suggest making this the default behaviour) Many thanks for a hint, Helmut Jarausch
2
7242
by: J. W. McCall | last post by:
I'm working on a MUD server and I have a thread that gets keyboard input so that you can enter commands from the command line while it's in its main server loop. Everything works fine except that if a player enters the 'shutdown' command, everything shuts down, but the input thread is still sitting waiting for enter to be pressed for raw_input. After enter is pressed, it exits back to the command prompt as it should. I'm wondering if...
0
1822
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run without issue? If not can you point me to a description of why. While updating objects on the screen I get a segfault after an indeterminate number of updates. It doesn't seem to matter how quickly the updates occur, but it does segfault faster...
1
2951
by: JerryKreps | last post by:
Hi, folks -- I'm a Python pup. As you can see from the session copied at the end of this post, I have the latest version of Python, and I've been using the Editor-Shell of the latest version of Boa Constructor while going through some Python tutorials. Everything was working as expected until I started using the raw_input built-in function. There seems to be some unreliable behavior in Boa Constructor's Editor - Shell. If you look...
21
8778
by: planetthoughtful | last post by:
Hi All, As always, my posts come with a 'Warning: Newbie lies ahead!' disclaimer... I'm wondering if it's possible, using raw_input(), to provide a 'default' value with the prompt? I would like to include the ability to edit an existing value (drawn from an SQLite table) using a DOS console Python app, but my gut
17
4203
by: Stuart McGraw | last post by:
In the announcement for Python-2.3 http://groups.google.com/group/comp.lang.python/msg/287e94d9fe25388d?hl=en it says "raw_input(): can now return Unicode objects". But I didn't see anything about this in Andrew Kuchling's "2.3 What's New", nor does the current python docs for raw_input() say anything about this. A test on a MS Windows system with a cp932 (japanese) default locale shows the object returned by raw_input() is a str()...
7
2050
by: Mike Kent | last post by:
It's often useful for debugging to print something to stderr, and to route the error output to a file using '2>filename' on the command line. However, when I try that with a python script, all prompt output from raw_input goes to stderr. Consider the following test program: === Start test.py === import sys
8
5321
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples. (Sorry, long email) The first two examples are behaving normal, the thirth is strange....... I wrote the following flabbergasting code: #-------------------------------------------------------------
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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
10216
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
10165
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
10002
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
9044
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...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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

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.