pyautogui
是一个跨平台的Python库,允许开发者通过代码控制鼠标、键盘以及进行屏幕图像识别。它用简单的语法模拟人类对计算机的操作,适合快速实现轻量级自动化任务,比如防止系统休眠、批量处理重复操作等。
moveTo(x, y)
click()
dragTo(x, y)
position()
typewrite("Hello!")
press('enter')
hotkey('ctrl', 'c')
screenshot('screen.png')
locateOnScreen('button.png')
(通过图像匹配坐标)如下:
import time
import pyautogui
def main():
MAX_RUNTIME = 3600 * 8 # 8小时自动停止
start_time = time.time()
while time.time() - start_time < MAX_RUNTIME:
pyautogui.moveRel(1,1)
# pyautogui.press('press')
pyautogui.press('esc',interval=0.5)
pyautogui.scroll(1)
pyautogui.hotkey('ctrl','f')
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
print(f"移动:{current_time}")
time.sleep(100)
if __name__=="__main__":
main()