title: Why is KeyboardInterrupt not working for Python pyautogui script? Alternative way to exit program/loop?
body: <p>I am trying to complete a simple GUI automation program that merely opens a web page and then clicks on a specific spot on the page every 0.2 seconds until I tell it to stop. I want my code to run and have its loop run infinitely until a keybind I specify breaks the loop (or entire program). I started out with the classic KeyboardInterrupt, which supposedly enables CTRL+C to exit a program. Here is my code:</p>
  
  <pre><code>import webbrowser, pyautogui, time
  webbrowser.open('https://example.com/')
  print('Press Ctrl-C to quit.')
  time.sleep(5)
  #pyautogui.moveTo(1061, 881)
  try:
      while True:
              time.sleep(0.2)
              pyautogui.click(1061,881)
  except KeyboardInterrupt:
      print('\nDone.')
  </code></pre>
  
  <p>Unfortunately, KeyboardInterrupt and using CTRL-C to exit do not seem to work for this script (likely due to the while loop?). This causes the loop to continue to run infinitely without a way to be stopped. So my questions are: why isn't the Keyboard Interrupt working? I've seen similar examples in other scripts. Additionally, if the KeyboardInterrupt doesn't work, is there a way I can code a simple keybind to exit the program/loop?</p>