Run Upon PyAutoGUI Fail Safe

Viewed 2142

Put simply, I'm trying to figure out how to run some code when the PyAutoGUI failsafe executes. I've tried searching this problem many times and can't figure out a way to do it.

This is what I want:

  1. Move mouse to corner and provoke failsafe.
  2. Right before program ends from fail safe, runs line of code.
  3. Program completely closes.
1 Answers

The pyautogui.FailSafeException is raised when the mouse is moved to the upper left corner (x,y of 0, 0). You could catch this exception and run code from there:

import pyautogui import sys

while True:
    try:
        pyautogui.moveTo() # Any PyAutoGUI (without side effects) call will do here.
    except pyautogui.FailSafeException:
        print('Running code before exiting.') # Your code here.
        sys.exit()
Related