python-script-library/鼠标旋转往左移动.py

82 lines
3.2 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pyautogui
import time
import keyboard
import random
# ===========================
# 🎮 配置区域(可调参数)
# ===========================
pyautogui.FAILSAFE = True # 启用安全机制:鼠标快速移到左上角可紧急停止!
SCREEN_WIDTH, SCREEN_HEIGHT = pyautogui.size()
SCREEN_HEIGHT_MIDDLE = SCREEN_HEIGHT // 2 # 鼠标保持在屏幕竖直中央Y轴不变
MOVE_STEP = 50 * 2 # 每次移动的像素距离(越小越慢越自然,比如 1~3
MOVE_DURATION = 0.5 * 2 * 2 # 每次移动持续时间(单位:秒,越大越慢越平滑,推荐 0.2~1.0
MOVE_DELAY = 0 # 每次移动后的停顿时间(可调整,推荐 0.02~0.1
USE_RANDOM_OFFSET = False # 是否加入随机偏移(你不需要,保持 False 即可)
CONTROL_KEY = 'F1' # 控制鼠标移动的快捷键可修改为F1-F12或其他键
# ===========================
# 🧠 状态控制
# ===========================
is_moving_left = False # 控制是否正在向左移动鼠标
# ===========================
# 🖱️ 鼠标向左移动函数
# ===========================
def auto_move_left():
global is_moving_left
print(f"🖱️ 【鼠标自动左移】已启动:鼠标将缓慢向左移动...")
while is_moving_left:
current_x, current_y = pyautogui.position() # 获取当前鼠标 x, y 坐标
target_x = max(current_x - MOVE_STEP, 0) # 往左移动,但不能小于 0屏幕最左边
target_y = current_y # Y 坐标保持不变,保持在屏幕中间或当前位置
# 移动鼠标到目标位置(向左),使用平滑动画
pyautogui.moveTo(target_x, target_y, duration=MOVE_DURATION)
# 可选的停顿,模拟自然观察
time.sleep(MOVE_DELAY)
print(f"🛑 【鼠标自动左移】已停止。")
# ===========================
# ⌨️ 按键监听:启动 / 停止 左移控制
# ===========================
def toggle_left_movement():
global is_moving_left
if is_moving_left:
is_moving_left = False
print(f"⏹️ {CONTROL_KEY} 被按下:鼠标左移已停止。")
else:
is_moving_left = True
print(f"▶️ {CONTROL_KEY} 被按下:鼠标开始缓慢向左移动!")
# 在新线程中运行,避免阻塞主线程和键盘监听
import threading
threading.Thread(target=auto_move_left, daemon=True).start()
# 注册热键监听
keyboard.add_hotkey(CONTROL_KEY, toggle_left_movement)
# ===========================
# 🚀 启动脚本 & 保持运行
# ===========================
print("🎮 脚本已启动!")
print(f"🔘 按下 {CONTROL_KEY} 键:启动 / 停止 鼠标缓慢向左移动")
print("💡 鼠标会从当前位置开始,向左缓缓移动,不会重置到屏幕边缘")
print("⚠️ 安全提示你仍然可以用鼠标快速移动到屏幕左上角来紧急停止FAILSAFE 机制)")
print(" 当前设置:缓慢、平滑、自然向左移动,无随机抖动")
try:
keyboard.wait() # 保持程序运行,监听按键
except KeyboardInterrupt:
print("\n🛑 脚本已通过 Ctrl+C 停止。")
finally:
is_moving_left = False # 确保退出时停止移动