import ctypes
import keyboard
import time
Constants for controlling the monitor power
MONITOR_OFF = 2
SC_MONITORPOWER = 0xF170
HWND_BROADCAST = 0xFFFF
def turn_off_display():
“”“Turn off the display.”“”
ctypes.windll.user32.SendMessageW(HWND_BROADCAST, 0x0112, SC_MONITORPOWER, MONITOR_OFF)
def turn_on_display():
“”“Placeholder for the code triggering the display to turn on.
Since most hardware will wake up on any keypress, this might be left empty.”“”
pass
def on_press_reaction(event):
“”“Function to react to keypress events. Turning the display off on ‘E’, and handling ‘R’ separately.”“”
if event.name == ‘e’:
print(“E pressed, turning off the display.”)
turn_off_display()
elif event.name == ‘r’:
No need to explicitly turn on the display as hardware handles it,
but you can execute related/needed code here if necessary.
print(“R pressed, display turns on by default behavior.”)
turn_on_display()
else:
Optionally handle other keys. For this particular setup, do nothing.
pass
if name == “main”:
print(“Press ‘E’ to turn the display off. Press ‘R’ to ‘wake’ the display (hardware behavior).”)
Set up the keyboard listener for press events
keyboard.on_press(on_press_reaction)
Keep the program running until it is explicitly stopped
try:
while True:
time.sleep(0.1) # Sleep to reduce CPU usage
except KeyboardInterrupt:
print(“Program exited gracefully.”)
this must work fast , and mous e movement must not wake screen nor any keys from keyboard must wake up screen