loop until keypress (Windows XP)

P

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;


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
 
F

Farshid Lashkari

placid said:
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
 
G

Gabriel Genellina

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
 
A

Andy Terrel

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
 
P

placid

Gabriel said:
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)
 
T

Thomas Guettler

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/Cookbook/Python/Recipe/134892

It does not loop, it waits until the key is pressed. I hope that is what
you want.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top