On Fri, 11 Feb 2005 17:26:04 +0100, BOOGIEMAN <BOOGIEMANPN@ya hoo.com> wrote:[color=blue]
> 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.[/color]
On 2005-02-11, BOOGIEMAN <BOOGIEMANPN@YA HOO.COM> wrote:[color=blue]
> On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:
>[color=green]
>> Err, why?[/color]
>
> It looks to ugly this way. I want to press
> any key without ENTER to continue[/color]
Like somebody already said: use the WConio module.
Somebody already posted a link.
I suggest you go look at it.
--
Grant Edwards grante Yow! ... the MYSTERIANS
at are in here with my
visi.com CORDUROY SOAP DISH!!
BOOGIEMAN wrote:[color=blue]
> On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:
>
>[color=green]
>>Err, why?[/color]
>
>
> It looks to ugly this way. I want to press
> any key without ENTER to continue[/color]
On Fri, Feb 11, 2005 at 05:37:19PM +0100, BOOGIEMAN wrote:[color=blue]
> On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:
> [color=green]
> > Err, why?[/color]
>
> It looks to ugly this way. I want to press
> any key without ENTER to continue[/color]
read the documentation on readline.
Hmm! it says "Availabili ty: Unix". Any particular reason? readline
should be fine on OSX and Win32
--
John Lenton (john@grulic.or g.ar) -- Random fortune:
sugar daddy, n.:
A man who can afford to raise cain.
[color=blue]
> Err, why?[/color]
[color=blue][color=green]
>> It looks to ugly this way. I want to press
>> any key without ENTER to continue[/color][/color]
BOOGIEMAN wrote:[color=blue]
> On Fri, 11 Feb 2005 18:00:08 +0100, den wrote:
>
>[color=green]
>>Did you try this:
>>
>>import msvcrt
>>msvcrt.getch( )[/color]
>
>
> Yes, that's what I need. Thank you.
> BTW, sometimes program continues
> without me pressing any button, why ?[/color]
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.
BOOGIEMAN wrote:[color=blue]
> On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:
>
>[color=green]
>>print prompt
>>while msvcrt.kbhit():
>> msvcrt.getch()
>>msvcrt.getch( )[/color]
>
>
> 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[/color]
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 | ncoghlan@email. com | Brisbane, Australia
---------------------------------------------------------------
BOOGIEMAN wrote:[color=blue]
> On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:
>[color=green]
>>print prompt
>>while msvcrt.kbhit():
>> msvcrt.getch()
>>msvcrt.getch( )[/color]
>
>
> Thanks, it works but without line "print prompt" and also[/color]
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.
[color=blue]
> 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[/color]
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>
On Sun, 13 Feb 2005 12:08:26 +1000, Nick Coghlan wrote:
[color=blue]
> 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.[/color]
Comment