stopping a while True: with the keyboard

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Sep 26 05:20:18 CEST 2007


Benjamin <musiccomposition at gmail.com> writes:

> On Sep 25, 8:19 pm, patrick <pured... at 11h11.com> wrote:
> > 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
> 
> Ctrl-C

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



More information about the Python-list mailing list