0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

スリープ回避するためにマウスを動かす

Posted at

スリープ回避プログラム

概要

このプログラムは、指定した時間の間、マウスカーソルを微妙に動かしてPCのスリープ状態への移行を防止します。
PyQt5でGUIを構築し、残り時間の表示や操作の中断が可能です。
※スリープ設定をいじればいいじゃんというのは本末転倒なのでここはあえてpythonでマウスを動かします。動かした後、サインアウトができるようになってます。


動作イメージ

  1. プログラム起動時に、スリープ回避動作の継続時間(秒数)を入力するダイアログが表示されます。
  2. 入力した時間をもとにメイン画面が表示され、残り時間が秒単位でカウントダウンされます。
  3. カウントダウン中はマウスカーソルが1秒ごとに微妙に動き、スリープを回避します。
  4. 「キャンセル」ボタンでスリープ回避動作をいつでも中断できます。
  5. 時間経過後は自動的に終了します。

動作環境

  • Python 3.x
  • PyQt5
  • pyautogui

インストール方法

  1. Pythonがインストールされていることを確認してください。

  2. 必要なパッケージをインストールします:

pip install PyQt5 pyautogui

プログラム

import sys
import pyautogui
from PyQt5.QtWidgets import (
    QApplication, QWidget, QVBoxLayout, QLabel,
    QPushButton, QInputDialog, QMessageBox,QCheckBox
)
from PyQt5.QtCore import QTimer
import subprocess

class SleepPreventer(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("スリープ回避プログラム")

        self.layout = QVBoxLayout()
        self.label = QLabel("残り時間: 0 秒")
        self.cancel_btn = QPushButton("キャンセル")
        self.cancel_btn.clicked.connect(self.cancel)

        self.layout.addWidget(self.label)
        self.layout.addWidget(self.cancel_btn)
        self.setLayout(self.layout)

        self.total_seconds = 0
        self.remaining_seconds = 0

        self.timer = QTimer()
        self.timer.timeout.connect(self.update_countdown)

        self.signout_checkbox = QCheckBox("処理終了後にサインアウトする")
        self.layout.addWidget(self.signout_checkbox)
        # 起動時に時間入力ダイアログを表示
        self.get_time_from_user()

    def get_time_from_user(self):
        seconds, ok = QInputDialog.getInt(
            self,
            "スリープ回避時間入力",
            "スリープまでの時間(秒)を入力してください:",
            min=1,
            max=86400,  # 最大24時間
            step=1,
        )
        if ok:
            self.total_seconds = seconds
            self.remaining_seconds = seconds
            self.label.setText(f"残り時間: {self.remaining_seconds}")
            self.timer.start(1000)  # 1秒毎に更新
        else:
            # 入力キャンセル時はプログラム終了
            self.close()

    def update_countdown(self):
        if self.remaining_seconds <= 0:
            self.timer.stop()
            QMessageBox.information(self, "完了", "スリープ回避動作を終了しました。")
            if self.signout_checkbox.isChecked():
                self.sign_out()
            self.close()
            return
        
        # 残り秒数更新とマウス動作は変わらず
        self.remaining_seconds -= 1
        self.label.setText(f"残り時間: {self.remaining_seconds}")
        self.move_mouse_slightly()

    def sign_out(self):
        try:
            subprocess.run(["shutdown", "/l"], check=True)
        except Exception as e:
            QMessageBox.warning(self, "エラー", f"サインアウトに失敗しました:\n{e}")

    def move_mouse_slightly(self):
        # 現在のマウス位置を取得
        x, y = pyautogui.position()

        # 微妙に動かす(例: 10ピクセル右へ、すぐ戻す)
        pyautogui.moveTo(x + 10, y)
        pyautogui.moveTo(x + 10, y+10)
        pyautogui.moveTo(x  , y + 10)
        pyautogui.moveTo(x, y)

    def cancel(self):
        self.timer.stop()
        QMessageBox.information(self, "中断", "スリープ回避動作を中断しました。")
        self.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = SleepPreventer()
    window.show()
    sys.exit(app.exec_())

注意事項

pyautoguiはマウス操作を制御するため、OSのマウス設定やセキュリティ設定により動作が制限される場合があります。

macOSでは、アクセシビリティ設定で「画面収録」や「補助機能」の許可が必要です。

長時間のスリープ回避はPCの省電力設定に影響を与える可能性がありますので、用途に応じて適切に設定してください。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?