473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

loop until keypress (Windows XP)

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

Aug 10 '06 #1
6 14376
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
Aug 10 '06 #2
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

Aug 10 '06 #3
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

Aug 10 '06 #4

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)

Aug 10 '06 #5
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.
Aug 10 '06 #6

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

Aug 10 '06 #7

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

Similar topics

36
by: Remi Villatel | last post by:
Hi there, There is always a "nice" way to do things in Python but this time I can't find one. What I'm trying to achieve is a conditionnal loop of which the condition test would be done at...
6
by: moti | last post by:
Whenever I use PrintDocument.Print() to print a page it goes to PrintDocument_PrintPage and stays there forever unless I set e.HasMorePages to False. When I set it to False it prints the page but...
0
by: Grayzag | last post by:
Hi there, As part of my Software course, i have to create a game. Since I originally started out with python, I was used to it being really easy to create a main loop to control the game with a...
2
by: lSugaRushl | last post by:
Hey all. New to the forum. Is there a way to make a loop continue until an input is entered? i've tried the - if (cin.peek() == '\n') {} - but it stays there til an input is added, just...
2
by: shogot99 | last post by:
Till here the info gotten is ok Loop Until strainfo = "" ScreenMNS.WaitHostQuiet (400) Second process Copy "B" & paste in "E" Range("B2:B600").Select
2
by: yogi_bear_79 | last post by:
I am working on divide-and-average lab. I everything done excep the syntax for the loop. The loop should run replaceing x with the resutls of (x + a/x )/2 until x, and (x + a/x )/2 differ by no...
1
by: pfildes | last post by:
Hi, I am pretty new to VB.net and am having trouble figuring out how to do this. I have an app that copies data from one datasource to another. I have it set to run through a for loop for...
1
by: Scott Dreadlock | last post by:
Hi, I'm creating a Tarot card randomizer in VB.net just for an excersize of trying to remain faithful to random while still using certain rules - like no duplicate values. Here's the code. I've...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.