Take the 2-minute tour ×

Alright this is a simple question, because i don't really know what its called..

Say i have a loop in python

if pattern in buffer:
        while logme == "y":
            logging.debug("Pre-Encrypted: %s" % buffer)
            print "Pre-Encrypted: %s" % buffer
            print "Pre-Encrypted: %s" % buffer

How can i make it so when i press a keyboard key like P while the loop is running and have it execute a command like pausing the loop, exiting, or doing anything? Like not command line arguments but while the actual program is running..

share|improve this question
I think you might need to use threading to keep your loop going while you get input in another thread, since both input() and raw_input() are blocking. –  IT Ninja Jan 18 at 1:27
add comment

2 Answers

You could use curses, which would be a bit complicated.

A quick hack around it would be to intercept SIGINT (Ctrl-C, KeyboardInterrupt) in Python.

def foo():
  catch KeyboardInterrupt:

In addition to violating expectations about Ctrl-C behavior, this also doesn't provide an obvious way to restart the thing.

share|improve this answer
add comment

Your Answer


