Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Version 5
- # NOTE: Windows users need to install pypiwin32 for this script to work:
- # Run CMD as administrator > pip install pywin32
- import platform
- def screenOff():
- system = platform.system()
- if system == 'Linux':
- import os
- import subprocess
- session = os.environ.get('XDG_SESSION_TYPE')
- if session == 'x11':
- # NOTE: To prevent passworded lockscreen from appearing for XFCE:
- # Start > Power Manager > Security:
- # Automatically lock the session: Never
- # [ ] Lock screen when system is going to sleep
- # SOURCE: https://stackoverflow.com/questions/60851689/turn-screen-off-in-linux-via-python3
- # SOURCE: https://askubuntu.com/questions/38776/trigger-screensaver-off-monitor
- # SOURCE: https://ubuntuforums.org/showthread.php?t=1607724
- # SOURCE: https://wiki.archlinux.org/title/Display_Power_Management_Signaling
- subprocess.run(['xset', 'dpms', 'force', 'suspend'])
- elif session == 'wayland':
- # NOTE: To prevent passworded lockscreen from appearing for KDE:
- # Start > Screen Locking:
- # Lock screen automatically: [ ] After [5 minutes]
- # [ ] After waking from sleep
- # SOURCE: https://askubuntu.com/questions/1316097/how-to-turn-off-the-monitor-via-command-on-wayland-kde-plasma
- subprocess.run(['kscreen-doctor', '--dpms', 'off'])
- else:
- print('Could not determine the display server.')
- elif system == 'Windows':
- # pip install pywin32
- import win32con
- import win32gui
- # SOURCE: https://stackoverflow.com/questions/70965202/python-turn-screen-on-and-off-on-windows
- win32gui.PostMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
- else:
- print('Unknown OS detected:', system)
- # ----
- import threading
- cancelled = threading.Event()
- import time
- def watch_escape():
- system = platform.system()
- if system == 'Windows':
- import msvcrt
- while not cancelled.is_set():
- if msvcrt.kbhit():
- key = msvcrt.getch()
- if key == b'\x1b': # Esc
- cancelled.set()
- return
- time.sleep(0.01)
- else:
- import select
- import sys
- import termios
- import tty
- fd = sys.stdin.fileno()
- old = termios.tcgetattr(fd)
- try:
- tty.setcbreak(fd)
- while not cancelled.is_set():
- ready, _, _ = select.select([sys.stdin], [], [], 0.1)
- if ready:
- if sys.stdin.read(1) == '\x1b':
- cancelled.set()
- return
- finally:
- termios.tcsetattr(fd, termios.TCSADRAIN, old)
- def countdown(message, seconds):
- while seconds > 0:
- if cancelled.is_set():
- return False
- print(f'\r{message.format(seconds)} Quit → Esc', end='', flush=True)
- for _ in range(10):
- if cancelled.is_set():
- return False
- time.sleep(0.1)
- seconds -= 1
- return True
- # ----
- def main():
- t = threading.Thread(target=watch_escape)
- t.start()
- # This delay is to give the mouse a chance to stop moving.
- # Mouse-wire tension + slippery feet can cause unintended mouse movement which will wake up the monitor prematurely.
- if not countdown('Turning off monitor in {} seconds . . .', 5):
- cancelled.set()
- t.join() # Wait for thread so terminal restores.
- print()
- return
- cancelled.set()
- t.join() # Wait for thread so terminal restores.
- screenOff()
- print()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment