랭귀지/pandas
화면보호기
유키공
2025. 6. 13. 08:12
from pynput import keyboard, mouse
from pynput.keyboard import Controller, Key
import threading
import time
from datetime import datetime
import ctypes
import os
import platform
# 설정
IDLE_TIME_LIMIT = 300 # 5분 이상 비활동 시
CHECK_INTERVAL = 10 # 활동 체크 주기
keyboard_controller = Controller()
last_activity_time = time.time()
# 사용자 입력 감지 핸들러
def on_input_activity(event):
global last_activity_time
last_activity_time = time.time()
# 화면보호기 방지를 위한 Shift 입력
def prevent_sleep_with_key():
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(f"[{now}] [INFO] 활동 없음: Shift 입력 시뮬레이션")
keyboard_controller.press(Key.shift)
time.sleep(0.1)
keyboard_controller.release(Key.shift)
# 화면 잠금
def lock_workstation():
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(f"[{now}] 🔒 화면 잠금 실행")
ctypes.windll.user32.LockWorkStation()
# 시스템 종료
def shutdown_system():
if platform.system() == "Windows":
print("[INFO] 시스템 종료 명령 실행")
os.system("shutdown /s /t 0")
# 시스템 재부팅
def reboot_system():
if platform.system() == "Windows":
print("[INFO] 시스템 재부팅 명령 실행")
os.system("shutdown /r /t 0")
# 활동 감지 쓰레드
def monitor_idle():
while True:
idle_time = time.time() - last_activity_time
if idle_time >= IDLE_TIME_LIMIT:
prevent_sleep_with_key()
time.sleep(CHECK_INTERVAL)
# 스케줄러 쓰레드
def scheduled_actions():
triggered = set()
while True:
now = datetime.now()
now_str = now.strftime('%Y-%m-%d %H:%M:%S')
current_time = now.strftime('%H:%M')
weekday = now.weekday() # 0:월 ~ 4:금
# 오전 11시 화면 잠금
if current_time == "11:00" and "lock" not in triggered:
lock_workstation()
triggered.add("lock")
# 오후 5시 종료/재부팅
if current_time == "17:00" and "shutdown" not in triggered:
if weekday in [0, 1, 2, 3]: # 월~목
print(f"[{now_str}] 🔁 월~목: 시스템 재부팅")
reboot_system()
elif weekday == 4: # 금요일
print(f"[{now_str}] ⏹️ 금요일: 시스템 종료")
shutdown_system()
triggered.add("shutdown")
# 자정이 되면 트리거 초기화
if current_time == "00:00":
triggered.clear()
time.sleep(5)
# 입력 감지 리스너 시작
keyboard.Listener(on_press=on_input_activity).start()
mouse.Listener(
on_move=on_input_activity,
on_click=on_input_activity,
on_scroll=on_input_activity
).start()
# 쓰레드 실행
threading.Thread(target=monitor_idle, daemon=True).start()
threading.Thread(target=scheduled_actions, daemon=True).start()
print("🛡️ 활동 감지 + 자동 잠금/재부팅/종료 프로그램 실행 중... (Ctrl+C로 종료)")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\n[프로그램 수동 종료]")
pyinstaller --noconsole --onefile idle_guard.py