stopping a while True: with the keyboard

P

patrick

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
 
P

patrick

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
 
A

Andy

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
 
B

Ben Finney

Benjamin said:

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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top