Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > loop until keypress (Windows XP)

Reply
Thread Tools

loop until keypress (Windows XP)

 
 
placid
Guest
Posts: n/a
 
      08-10-2006
Hi all,


Im using the cmd module and i have command that loops and keeps on
printing text, what i want to be able to do is loop until the user
presses a particular key, say Q/q ? I tried the following code;


line = raw_input ("press q to abort")
while line[0] != "q":
""" keep printing text """
line = raw_input ("press q to abort")

but raw_input blocks for input until the newline char. So i then tried
the following code


import sys

chr = sys.stdin.read(1)
while chr != "q":
""" keep printing text """
chr = sys.stdin.read(1)

but again this blocks too.

is there a way to do this, wait for user input but dont block? I could
use a thread that just does the previous code block but i already have
three Thread classes, its just getting too complex with threads!

Cheers

 
Reply With Quote
 
 
 
 
Farshid Lashkari
Guest
Posts: n/a
 
      08-10-2006
placid wrote:
> is there a way to do this, wait for user input but dont block?


Hi,

The msvcrt module should do what you want. Here is a sample:

import msvcrt

chr = 0
while chr != 'q':
""" keep printing text """
if msvcrt.kbhit():
chr = msvcrt.getch()


Keep in mind that this will only work on windows.

-Farshid
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-10-2006
At Thursday 10/8/2006 02:19, placid wrote:

>chr = sys.stdin.read(1)
>while chr != "q":
> """ keep printing text """
> chr = sys.stdin.read(1)
>
>but again this blocks too.
>
>is there a way to do this, wait for user input but dont block? I could
>use a thread that just does the previous code block but i already have
>three Thread classes, its just getting too complex with threads!


If your script only needs to be run on Windows -as the subject
suggests- you can use the msvcrt module:

from msvcrt import kbhit,getch

stop = False
while not stop:
print "Hello world!"
if kbhit(): stop = getch()=='q'

kbhit() is used to detect when a keypress is waiting, so the next
getch() will not block.



Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

 
Reply With Quote
 
Andy Terrel
Guest
Posts: n/a
 
      08-10-2006
If you did want a linux version you could just make people send a
KeyboardInterupt.


try:
print "Press ^C to stop"
loop
except KeyboardInterrupt:
some stop action or just pass

 
Reply With Quote
 
placid
Guest
Posts: n/a
 
      08-10-2006

Gabriel Genellina wrote:
> At Thursday 10/8/2006 02:19, placid wrote:
>
> >chr = sys.stdin.read(1)
> >while chr != "q":
> > """ keep printing text """
> > chr = sys.stdin.read(1)
> >
> >but again this blocks too.
> >
> >is there a way to do this, wait for user input but dont block? I could
> >use a thread that just does the previous code block but i already have
> >three Thread classes, its just getting too complex with threads!

>
> If your script only needs to be run on Windows -as the subject
> suggests- you can use the msvcrt module:
>
> from msvcrt import kbhit,getch
>
> stop = False
> while not stop:
> print "Hello world!"
> if kbhit(): stop = getch()=='q'
>
> kbhit() is used to detect when a keypress is waiting, so the next
> getch() will not block.


Thanks for the solution ive got it working. You were correct, the
script needs to run only on Windows.

Cheers (and thanks all for the replies)

 
Reply With Quote
 
Thomas Guettler
Guest
Posts: n/a
 
      08-10-2006
Am Wed, 09 Aug 2006 22:19:24 -0700 schrieb placid:

> Hi all,
>
>
> Im using the cmd module and i have command that loops and keeps on
> printing text, what i want to be able to do is loop until the user
> presses a particular key, say Q/q ? I tried the following code;
>


There is a portable getch() implementation:

http://aspn.activestate.com/ASPN/Coo.../Recipe/134892

It does not loop, it waits until the key is pressed. I hope that is what
you want.
 
Reply With Quote
 
placid
Guest
Posts: n/a
 
      08-10-2006

Thomas Guettler wrote:
> Am Wed, 09 Aug 2006 22:19:24 -0700 schrieb placid:
>
> > Hi all,
> >
> >
> > Im using the cmd module and i have command that loops and keeps on
> > printing text, what i want to be able to do is loop until the user
> > presses a particular key, say Q/q ? I tried the following code;
> >

>
> There is a portable getch() implementation:
>
> http://aspn.activestate.com/ASPN/Coo.../Recipe/134892
>
> It does not loop, it waits until the key is pressed. I hope that is what
> you want.



i want a read operation that doesn't block and im not concerned if my
program is Windows only, which is. (im using wmi module too).

msvcrt.kbhit() and msvcrt.getch() is what i need!


Cheers

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Pattern detection and until loop in perl vrushali Software 1 12-15-2010 06:08 PM
loop until two doubles differ by 0.00001 yogi_bear_79 C++ 2 03-11-2008 02:34 AM
Do something until keypress Peter Szinek Ruby 1 10-26-2006 12:40 PM
Loop until condition is true Remi Villatel Python 40 06-23-2005 07:21 PM



Advertisments