Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

stopping a while True: with the keyboard

1 view
Skip to first unread message

patrick

unread,
Sep 25, 2007, 9:19:08 PM9/25/07
to pytho...@python.org
hi all,

i am looking for a way to break a while True: when pressing "s" on my
keyboard. how can i do this?

pat

Benjamin

unread,
Sep 25, 2007, 10:48:18 PM9/25/07
to

Ctrl-C

patrick

unread,
Sep 25, 2007, 10:58:12 PM9/25/07
to pytho...@python.org
i don't want to quit my program only get out from a while True:
also i want to use only 1 key like s.

pat

Andy

unread,
Sep 25, 2007, 11:11:43 PM9/25/07
to
Hi,

I think you can use the 'curses' module to catch the keyboard event.
The name of the method is 'keyname()'. But I'm afraid curses is not
available on Windows Python, so you may have to be a bit more
imaginative. I just tried with Python 2.4 on Windows XP.

To leave the while loop, use the 'break' command.

Andy

Ben Finney

unread,
Sep 25, 2007, 11:20:18 PM9/25/07
to
Benjamin <musiccom...@gmail.com> writes:

That's not a very helpful response, since it doesn't address the OP's
request.

Patrick, the operating system "interrupt current process" keyboard
signal will cause Python to raise a 'KeyboardInterrupt' exception,
which unless caught will propagate back to the top-level Python
process and exit the program with a traceback.

On most operating systems, you can send this signal to the current
foreground process with the key combination Ctrl+C. I think that's
what Benjamin's laconic response is implying.

As for "detect a specific keypress", there's no OS-independent way to
do that. You'll need to write code that depends on a particular way of
getting at keypresses that won't work on all operating systems.

--
\ "Those who write software only for pay should go hurt some |
`\ other field." -- Erik Naggum, in _gnu.misc.discuss_ |
_o__) |
Ben Finney

Gabriel Genellina

unread,
Sep 26, 2007, 12:19:47 AM9/26/07
to pytho...@python.org
En Tue, 25 Sep 2007 22:19:08 -0300, patrick <pure...@11h11.com> escribi�:

> i am looking for a way to break a while True: when pressing "s" on my
> keyboard. how can i do this?

Windows only: Using the msvcrt module:

from msvcrt import kbhit, getch
a, b = 1, 1
while True:
a, b = a+b, a
print a
if kbhit() and getch() in 'sS':
print "\nThanks!"
break

There is a portable getch recipe in the Python Cookbook:
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892> that
supposedly should work on Windows, Linux and Mac OSX too.

If you just want to break the loop, and don't care too much in which state
your variables remain, you can use Ctrl-C and catch the KeyboardInterrupt:

a, b = 1, 1
try:
while True:
a, b = a+b, a
print a
except KeyboardInterrupt:
print "\nThanks!"
... continue program ...

--
Gabriel Genellina

Lawrence D'Oliveiro

unread,
Sep 26, 2007, 3:05:18 AM9/26/07
to
In message <mailman.1071.1190769...@python.org>, patrick
wrote:

> i am looking for a way to break a while True: when pressing "s" on my
> keyboard. how can i do this?

<http://groups.google.co.nz/groups?selm=fba8bo$331$1...@lust.ihug.co.nz>

0 new messages