473,667 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14391
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
KeyboardInterup t.
try:
print "Press ^C to stop"
loop
except KeyboardInterru pt:
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
42413
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 the end so the loop is executed at least once. It's some way the opposite of "while". So far, all I got is:
6
4602
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 is closes the print file. The effect is that it prints all my pages but creates a print job for each page. So if I print 100 pages I get 100 print jobs. The only way I went around this was to put all my page-filling code within...
0
2154
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 simple while statement, pausing where input is needed from the user. Now that I have moved on to VB, I can seem to find a way to pause the while statements until the user does something (e.g. clicks a button). When i start my app, if just throws...
2
2057
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 like a regular cin >> <variable>
2
1465
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
1511
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 more than a given amout, for example 0.00001. I've tried various ideas, the closest I came was while(x - divavg(a, x) != 0), which more or les sworked. It stoppend the loop when x = (x + a/x )/2, but I'm not sure that exactly meets the goal.
1
1081
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 each row in SourceDataTable. for each row i get the a value that is not an ID and I have a funciton that compares this to a column in the Destination, and i recieve the ID back if it exits and nothing if it doesn't. This work fine the next part...
1
1353
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 attempted to do this already as you can see from the code but the program simply stops half way through. I've also tried the same code with just one do-while loop surrounding all of the for-next loops and this simply doesn't work - there are duplicates...
0
8457
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8365
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7390
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.