自动移动鼠标防休眠的脚本

wen 实用脚本 1

本文目录导读:

自动移动鼠标防休眠的脚本

  1. 方案一:Python 脚本(跨平台)
  2. 方案二:PowerShell 脚本(Windows专属)
  3. 方案三:AutoHotkey 脚本(Windows)
  4. 方案四:批处理文件(Windows简易版)
  5. 安装依赖
  6. 使用建议
  7. 高级版本(带系统空闲检测)

我来为你提供几种自动移动鼠标防止系统休眠的脚本方案。

Python 脚本(跨平台)

基础版本

import pyautogui
import time
import random
def prevent_sleep():
    print("鼠标防休眠脚本已启动,按 Ctrl+C 停止")
    try:
        while True:
            # 获取当前鼠标位置
            x, y = pyautogui.position()
            # 向右移动5个像素
            pyautogui.moveTo(x + 5, y, duration=0.1)
            time.sleep(1)
            # 移动回来
            pyautogui.moveTo(x, y, duration=0.1)
            time.sleep(random.uniform(50, 70))  # 50-70秒移动一次
    except KeyboardInterrupt:
        print("\n脚本已停止")
if __name__ == "__main__":
    prevent_sleep()

随机移动版本(更智能)

import pyautogui
import time
import random
def smart_prevent_sleep():
    print("智能防休眠脚本运行中...")
    screen_width, screen_height = pyautogui.size()
    try:
        while True:
            # 生成随机目标位置(避免误触)
            target_x = random.randint(100, screen_width - 100)
            target_y = random.randint(100, screen_height - 100)
            # 平滑移动鼠标
            pyautogui.moveTo(target_x, target_y, duration=0.5)
            # 随机间隔(30-90秒)
            wait_time = random.uniform(30, 90)
            print(f"移动到 ({target_x}, {target_y}),等待 {wait_time:.1f} 秒")
            time.sleep(wait_time)
    except KeyboardInterrupt:
        print("\n脚本已停止")
if __name__ == "__main__":
    smart_prevent_sleep()

PowerShell 脚本(Windows专属)

# prevent_sleep.ps1
Add-Type @'
using System;
using System.Runtime.InteropServices;
public class MouseMover {
    [DllImport("user32.dll")]
    public static extern bool SetCursorPos(int X, int Y);
}
'@
Write-Host "防休眠脚本运行中,按 Ctrl+C 停止..." -ForegroundColor Green
while ($true) {
    $pos = [System.Windows.Forms.Cursor]::Position
    [MouseMover]::SetCursorPos($pos.X + 1, $pos.Y)
    Start-Sleep -Milliseconds 100
    [MouseMover]::SetCursorPos($pos.X, $pos.Y)
    Start-Sleep -Seconds 30
}

AutoHotkey 脚本(Windows)

创建文件 prevent_sleep.ahk

#Persistent
SetTimer, MoveMouse, 30000  ; 每30秒执行一次
return
MoveMouse:
    MouseGetPos, X, Y
    MouseMove, X+1, Y, 0
    MouseMove, X, Y, 0
return
^Esc::ExitApp  ; 按 Ctrl+Esc 退出脚本

批处理文件(Windows简易版)

创建 prevent_sleep.bat

@echo off
echo 闪烁 Scroll Lock 键防止休眠...
powershell -command "Add-Type -AssemblyName System.Windows.Forms; $w=New-Object System.Windows.Forms.SendKeys; while($true){$w.SendKeys('{SCROLLLOCK}'); Start-Sleep -Seconds 30}"
pause

安装依赖

Python脚本安装:

# 安装依赖
pip install pyautogui pygetwindow
# 运行脚本
python prevent_sleep.py

使用建议

  1. 选择合适时间间隔:根据实际需求调整移动频率(30-90秒较合适)
  2. 组合键方案:配合键盘按键(如 Scroll Lock)更隐蔽
  3. 智能检测:可添加检测系统空闲状态的逻辑

高级版本(带系统空闲检测)

import pyautogui
import time
import random
import ctypes
def system_idle_time():
    """获取系统空闲时间(Windows)"""
    class LASTINPUTINFO(ctypes.Structure):
        _fields_ = [("cbSize", ctypes.c_uint), ("dwTime", ctypes.c_uint)]
    info = LASTINPUTINFO()
    info.cbSize = ctypes.sizeof(info)
    if ctypes.windll.user32.GetLastInputInfo(ctypes.byref(info)):
        return (ctypes.windll.kernel32.GetTickCount() - info.dwTime) / 1000.0
    return 0
def intelligent_prevent():
    print("智能防休眠脚本(带空闲检测)")
    try:
        while True:
            idle_time = system_idle_time()
            if idle_time > 120:  # 空闲超过2分钟才移动
                pyautogui.moveRel(10, 0, duration=0.2)
                pyautogui.moveRel(-10, 0, duration=0.2)
                print(f"检测到空闲 {idle_time:.1f}秒, 已移动鼠标")
            time.sleep(random.uniform(20, 40))
    except KeyboardInterrupt:
        print("\n脚本已停止")
if __name__ == "__main__":
    intelligent_prevent()

选择最适合你需求的方案运行即可,建议先用测试间隔运行观察效果,再调整为最终频率。

抱歉,评论功能暂时关闭!