0

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 ?

PNxZEN
  • 19
  • 1
  • 5
  • 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

1 Answers1

0

Try using Asyncio instead of time?

Not for sure if this will work, but if not, reply to this and I will work on it more.

Edit: Nvm asyncio can only work with an async. Lemme do a few tests.

Another edit: Sorry but I cannot figure this out. Maybe join the python disc?

CloovyYT
  • 1
  • 1