Consider the script
#include WinEvent.ahk
WinEvent.Close((*) => MsgBox("notepad closed"), "ahk_exe notepad.exe")
To reproduce:
- Open notepad.exe on the first virtual desktop
- Switch to the second desktop (a message box should appear)
- Switch back to the first desktop and close notepad
- No message box appears.