# Function to monitor active window

 

def monitor_active_window():

last_window = ""

while True:

     try:

         hwnd = win32gui.GetForegroundWindow()

         _, pid = win32process.GetWindowThreadProcessId(hwnd)

            process = psutil.Process(pid)

            window_title = process.name()

         if window_title != last_window:

                last_window = window_title

                print(f"Window change detected: {window_title}")

                screenshot_path = take_screenshot()

                send_email("Tab Change Alert", f"A tab/window change was detected: {window_title}", screenshot_path)

                os.remove(screenshot_path)

     except Exception as e:

            print(f"Error monitoring active window: {e}")

        time.sleep(1)

 

 

Explanation :

       This monitor_active_window function continuously monitors the currently active window on the system, detects when it changes, and takes specific actions such as sending an email alert and taking a screenshot. Here’s how it works:


Functionality