Alternative to raw_input ?

B

BOOGIEMAN

I need something like "Press any key to continue" code for my program.
Currently I use : raw_input("Press Enter to continue ") but it's lame.
 
S

Simon Brunning

I need something like "Press any key to continue" code for my program.
Currently I use : raw_input("Press Enter to continue ") but it's lame.

Err, why?
 
G

Grant Edwards

It looks to ugly this way. I want to press
any key without ENTER to continue

Like somebody already said: use the WConio module.

Somebody already posted a link.

I suggest you go look at it.
 
J

John Lenton

It looks to ugly this way. I want to press
any key without ENTER to continue

read the documentation on readline.

Hmm! it says "Availability: Unix". Any particular reason? readline
should be fine on OSX and Win32

--
John Lenton ([email protected]) -- Random fortune:
sugar daddy, n.:
A man who can afford to raise cain.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFCDOLjgPqu395ykGsRAig6AJ9extU/bMx7GbBs1DGrWKkfAx8bhACgiE5w
jhda3sNi9vgHa7+x2vau4Z8=
=qbJW
-----END PGP SIGNATURE-----
 
B

BOOGIEMAN

Did you try this:

import msvcrt
msvcrt.getch()

Yes, that's what I need. Thank you.
BTW, sometimes program continues
without me pressing any button, why ?
 
P

Peter Hansen

BOOGIEMAN said:
Yes, that's what I need. Thank you.
BTW, sometimes program continues
without me pressing any button, why ?

Probably because you had already hit a key earlier, and
it was still in the keyboard queue.

You can flush it first:

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

Pack that up in a subroutine and call it when
you need to pause and continue only when the user
has seen your prompt. Any previous keystrokes
will be discarded by the loop, then it will wait
for a new one to be hit.


-Peter
 
B

BOOGIEMAN

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
 
N

Nick Coghlan

BOOGIEMAN said:
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="Press 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!")
 
P

Peter Hansen

BOOGIEMAN said:
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
 
B

BOOGIEMAN

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
 
F

Francis Girard

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
 
L

Lars

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
 
M

Michael Hoffman

Francis said:
[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.
 
S

Simon Brunning

It looks to ugly this way. I want to press
any key without ENTER to continue

You'll only got your users complaining that they haven't got an 'any' key...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top