faqts : Computers : Programming : Languages : PHP : Common Problems : Files : Tips and Tricks : Event Handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

33 of 68 people (49%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How can I trap a keyboard interrupt using Python?
Ctrl-Break does not raise KeyboardInterrupt, how can it be caught?

Mar 2nd, 2000 15:34
Nathan Wallace, James Spears, Oleg Orlov


import sys

def Worker(argv):
  while 1:
    pass

try:
  if __name__ =='__main__':
    Worker(sys.argv)
except KeyboardInterrupt:
  print "Saving state..."