前阵子因安全软件原因,笔记本锁屏无法修改,电脑锁屏时间非常短,写了个python小程序模拟鼠标移动,防止锁屏,结果没成功后来加入模拟点击、模拟输入,最后才防止锁屏。
代码中模拟输入其实不需要,代码加了注释。
from pymouse import PyMouse
from pykeyboard import PyKeyboard
from win32api import GetSystemMetrics
import random
import time
import pyautogui
pyautogui.FAILSAFE = False
m = PyMouse()
k = PyKeyboard()
m.position()
width = GetSystemMetrics(0)
heigth = GetSystemMetrics(1)
m.move(100, 100)
def prevent_screensaver():
while True:
x = random.randint(0, width)
y = random.randint(0, heigth)
m.move(x, y)
time.sleep(random.randint(3,5))
m.click(1731, 646) #防止锁屏
#k.type_string('hello')
#k.tap_key(k.enter_key)
pyautogui.moveTo(x, y, duration=1.25)
if __name__ == "__main__":
prevent_screensaver()
注:
可以使用pyinstaller -Fw将上述代码发面为mouse.exe程序
可以写一个bat文件用于终止上述可执行程序 taskkill /f /t /im mouse.exe
如果报一些关于“from windows import PyMouse, PyMouseEvent” 的错误,请安装相应包
pip install pyuserinput