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..

user1734291
  • 7
  • 1
  • 2
  • 6
  • 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 '13 at 01:27

2 Answers2

1

Use Getch

See Python read a single character from the user

Or use curses

Community
  • 1
  • 1
Alex L
  • 8,748
  • 5
  • 49
  • 75
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.

tsm
  • 3,598
  • 2
  • 21
  • 35