M
M
Maxim Zubenko2021-02-11 12:52:26
Python
Maxim Zubenko, 2021-02-11 12:52:26

How to track when the monitor turned off?

Hello guys, does anyone know if it's possible to track when the monitor is off?

I am writing for myself a kind of control over what children do on the computer. I have already implemented which programs, when they start and when they turn off. I have already realized when and how much which window (what program) was used.

I learned to track when the "screen saver" (screensaver) starts and measure how long the computer was in this screen saver mode.

But I encountered the fact that after 15 minutes of inactivity, the computer turns off the monitor and (as it turned out) at the moment the monitor is turned off, it also turns off the screensaver. At the same time (in the background with the monitor turned off), the focus is transferred to the last used window (the browser there or PyCharm) and the statistics begin to assume that we are using PyCharm, but in fact the monitor is turned off and no one uses anything.

The question is how to track this moment, i.e. check, they say, if the screen saver is turned off, then check, "maybe the monitor is turned off." This is the moment I can't track down.

ps I implemented almost everything through pywin32

ps2. Here is an example (shortened but working function for screensaver tracking)

import datetime
from time import sleep
from win32gui import GetWindowText, GetForegroundWindow
from win32com.client import GetObject

_wmi = GetObject('winmgmts:')
pause_ = .5
just_ = 25

def pause_screensaver():
    is_screensaver = False
    while len(_wmi.ExecQuery("Select * from win32_process Where Name LIKE '%.scr'")) > 0:
        is_screensaver = True
        sleep(pause_)
    return is_screensaver

...
            start_screen_time = datetime.datetime.today()
            if pause_screensaver():
                end_screen_time = datetime.datetime.today()
                diff_ = str(end_screen_time - start_screen_time).split('.')[0]
                print("-- screensaver --".ljust(just_ - 5), diff_)
...


And when the "pause_screensaver" function is triggered, it spins endlessly while the screen saver is on, and then bam and the monitor turns off, while the loop exits and then the program probes the active window again (via GetForegroundWindow()) and (having detected it) starts thinking that we are using this window again, although in fact the monitor is turned off.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question