I don't remember doing this but I apparently answered this exact question.
Call Standby_Main() in your loops before or after each SYNC. Put the globals at the beginning of the program. Call Standby_Init() at the beginning of your program.
Global standby_Level
Global standby_hwnd
Global standby_ScreenCheckTime
`Get and store the "window handle" for Standby Mode
Function Standby_Init()
load dll "user32.dll",1
standby_hwnd = call dll(1,"GetActiveWindow")
EndFunction
Rem If the Games Window is not the active window, CPU friendly loop until it is.
Function Standby_Main()
`Throttle "Screen is active check" to at most once per second (to be efficient)
If Timer() - standby_ScreenCheckTime > 1000
`Get current active window from Windows
hwnd = call dll(1,"GetActiveWindow")
`If active window doesnt match games window then enable standby mode
if hwnd <> standby_hwnd Then standby_Level = 1
`record check time
standby_ScreenCheckTime = Timer()
EndIf
`Enter Standby Mode CPU Friendly Loop if Standby is enabled
While standby_Level = 1
Sync Sleep 1
`Throttle "Screen is active check" to at most once per second
If Timer() - standby_ScreenCheckTime > 1000
`Get current active window from Windows
hwnd = call dll(1,"GetActiveWindow")
`If active window matches game window then abort standby
if hwnd = standby_hwnd
standby_Level = 0
`restore sync timing
Sync Sleep 0
endif
`record check time
standby_ScreenCheckTime = Timer()
EndIf
EndWhile
EndFunction
This not only detects loss of focus but also reduces CPU and GPU usage to zero.
Read more about implementing standby mode here:
https://forum.thegamecreators.com/thread/205649