Search notes:

Python type: KeyboardInterrupt

KeyboardInterrupt inherits from BaseException
The following script prints a number in the range from 0 through 99999 every second.
Pressing ctrl+c prints the current time and continues printing the numbers.
Pressing ctrl+c twice within a second exits the script:
import time
from datetime import datetime

try:
   for i in range(100000):
      try:
         print(i)
         time.sleep(1)
      except KeyboardInterrupt as k:
         print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
         time.sleep(1)
      
except KeyboardInterrupt:
   print('exiting')

Index