0

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.basicConfig(filename='hook.log',level=logging.DEBUG)
            logging.debug("Pre-Encrypted: %s" % buffer)
            print "Pre-Encrypted: %s" % buffer
        else:
            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..

1
  • 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. Commented Jan 18, 2013 at 1:27

2 Answers 2

1

Use Getch

See Python read a single character from the user

Or use curses

Sign up to request clarification or add additional context in comments.

Comments

1

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():
  try:
     long_running_process()
  catch KeyboardInterrupt:
     deal_with_interrupt()

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.