LoginSignup
0
1

More than 1 year has passed since last update.

スリープを抑止する、画面を消す(Python,Windows)

Last updated at Posted at 2021-09-27

とりあえず

スリープ抑止

import ctypes
ES_CONTINUOUS = 0x80000000
ES_AWAYMODE_REQUIRED = 0x00000040
ES_SYSTEM_REQUIRED = 0x00000001
ES_DISPLAY_REQUIRED = 0x00000002

def denysleep(): #スリープを抑止する
    ctypes.windll.kernel32.SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_CONTINUOUS)

def allowsleep(): #スリープを再開させる
    ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS)

とりあえず作ってみたけどちゃんと動くか調べてない。

画面を消す

import win32api
WM_SYSCOMMAND = 0x0112
SC_MONITORPOWER = 0xF170

def displaysuspend(): #モニタを強制的にオフにする
    win32api.PostMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, 2)

Pywin32が必要
オフには簡単にできるが、オンにするのは非常に手間がかかる、メッセージキューに単にモニターオンにしろと流しても動作しないらしい
pyautoguiを使うのが多分ベスト

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1