I think what you are asking for is screensaver events. There is some code that is close to this
here[
^]. You want to monitor for example SC_SCREENSAVE and SC_MONITORPOWER events, perhaps others.
Also check out
this[
^] link and related answers...