Search code examples
pythonwindowsmonitor

How to know the status of monitor is on or off in python?


How to know the status of monitor is on or off in python?

I would like to receive a beep notification every 50 minutes of every hour like this:

import datetime
import time
import winsound

frequency = 2500 # Set Frequency To 2500 Hertz
duration = 200 # Set Duration To 1000 ms == 1 second
while True:
     # If the monitor is Black by sleep mode
     if ???:
         time.sleep(60)
         continue
     now = datetime.datetime.now()
     if now.minute == 50:
         print(now)
         winsound.Beep(frequency, duration)
         time.sleep(60)

However, I don't want to receive a beep notification when the monitor is off because the monitor is on when I'm working, and the monitor is off because it's in power saving mode when I'm not.

But I don't know how to tell if the monitor is on or off.

How can I know the status of my monitor?

best regards!

I Solved like this

import time
from datetime import datetime

import win32api
import winsound

frequency = 1500
duration = 100


def getIdleTime():
    return (win32api.GetTickCount() - win32api.GetLastInputInfo()) / 1000.0


while True:
    now = datetime.now()
    if getIdleTime() > 3600:
        continue
    elif now.minute == 50:
        winsound.Beep(frequency, duration)

    time.sleep(60)
``

Solution

  • As stated in this old answer there is no reliable way to check the status of a monitor. But i think you don't have to rely on monitor state. Check inactivity time is a better approach.