2

Directly into the problem, I was trying to implement the screen/display/monitor Off and On feature into my primary program. I researched a bit and found this one answer interesting. So, tried testing it. Here's the code in a nutshell:

import time
import win32gui
import win32con

def ScreenOFF():
    """
    Function to turn off the screen.
    """
    return win32gui.SendMessage(win32con.HWND_BROADCAST,
                            win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)

def ScreenON():
    """
    Function to turn on the screen.
    """
    return win32gui.SendMessage(win32con.HWND_BROADCAST,
                            win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, -1)

ScreenOFF()
time.sleep(5)
ScreenON()
time.sleep(5)

The Screen Off was working great but while executing the Screen On function, The Screen only turns on for a second and then again it turns off immediately. I could not even reason now why that happens !

Also tried this more primitive approach but here too is the same problem:

import time
import ctypes

def ScreenOFF():
    """
    Function to turn off the screen.
    """
    ctypes.windll.user32.SendMessageW(65535, 274, 61808, 2)

def ScreenON():
    """
    Function to turn on the screen.
    """
    ctypes.windll.user32.SendMessageW(65535, 274, 61808, -1)

ScreenOFF()
time.sleep(5)
ScreenON()

Here's another reference link that might help here.

There are github repos on screen off, like this one, but NONE on Screen On !

Please suggest me if there are any fixes to this or other better ways to turn the screen On/OFF ?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
PNxZEN
  • 51
  • 2
  • 6
  • Any answer suggesting HWND_BROADCAST is incorrect. – Anders Feb 03 '22 at 02:31
  • You can pass 1 as parameter. It is for low power, but it will work. Most answers around suggest to fake a mouse movement. – azelcer Feb 03 '22 at 04:35
  • Passing 1 does not seem to work on my laptop, and I also tried faking mouse movement using `pyautogui.move(1,1)`, but that does work either... – PNxZEN Feb 03 '22 at 05:29
  • If possible, please upvote this question to reach as many users as possible... – PNxZEN Feb 03 '22 at 05:41

2 Answers2

0

Not a actual solution to the issue where it just turns as soon as it turns on, but I found a way to keep it not going off.

Not a very great way but it works

import time
import ctypes
import win32api, win32con
def screen_off():
    ctypes.windll.user32.SendMessageW(65535, 274, 61808, 2)
def screen_on():
    ctypes.windll.user32.SendMessageW(65535, 274, 61808, -1)
    move_cursor()
def move_cursor():
    x, y = (0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, x, y)

screen_off()
time.sleep(3)
screen_on()

If you move the cursor or type something the screen will stay on, so

eroc123
  • 624
  • 1
  • 4
  • 14
  • 1
    for me on my laptop the code works perfectly as intended. The use of `win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, x, y)` instead of `pyautogui.move(1,1)` seems to do the trick. – eroc123 Aug 28 '22 at 05:36
  • Thanks... `win32api` does the Trick :> – PNxZEN Sep 17 '22 at 07:03
  • Is it possible to only turn off one specific monitor? For instance: I have 3Monitors, would I be able to write a Python script to only turn off the lateral two? – Kelbig Oct 18 '22 at 20:03
0

There is a python package called monitorcontrol that you can use to control monitor power mode, contrast, input source and brightness (luminance). Here is example to power on and off monitor:

from monitorcontrol import get_monitors
from time import sleep

for monitor in get_monitors():
     with monitor:
         monitor.set_power_mode(4) # soft off
         sleep(3)
         monitor.set_power_mode(1) # on

If you want to use ctypes instead of this package see this answer to turn on and off the monitor with ctypes and windows monitor api

mohammad H
  • 21
  • 3