adb发送点击模拟-wjdr

import subprocess
import random
import time
import keyboard
import tkinter as tk
import threading
import configparser
from tkinter import font

# 创建ConfigParser对象
config = configparser.ConfigParser()

# 读取config.ini文件
config.read('config.ini')

# 从配置文件中获取坐标和范围
x1_base = config.getint('Coordinates', 'x1_base')
y1_base = config.getint('Coordinates', 'y1_base')
x2_base = config.getint('Coordinates', 'x2_base')
y2_base = config.getint('Coordinates', 'y2_base')
x3_base = config.getint('Coordinates', 'x3_base')
y3_base = config.getint('Coordinates', 'y3_base')
x_range = config.getint('Coordinates', 'x_range')
y_range = config.getint('Coordinates', 'y_range')

# 构建ADB点击命令模板
adb_click_template = "adb shell input tap {} {}"

# 初始化循环控制开关
click_loop_enabled = False
current_choice = None

# 执行ADB命令的函数
def run_adb_command(command):
    try:
        subprocess.run(command, shell=True, check=True)
    except subprocess.CalledProcessError as e:
        print(f"ADB命令执行出错: {e}")

# 生成随机睡眠时间的函数
def random_sleep():
    return random.uniform(0.01, 1.01)

# 生成x和y的随机偏移量的函数
def random_offset(x_range, y_range):
    x_offset = random.uniform(-x_range, x_range)
    y_offset = random.uniform(-y_range, y_range)
    return int(x_offset), int(y_offset)

# 定义执行点击操作的函数
def click_loop(x_base, y_base):
    global click_loop_enabled
    while click_loop_enabled:
        x_offset, y_offset = random_offset(x_range, y_range)
        x, y = x_base + x_offset, y_base + y_offset
        run_adb_command(adb_click_template.format(x, y))
        click_interval = random_sleep()
        time.sleep(click_interval)
        print(f"模式:({current_choice}),点击坐标: ({x}, {y}),间隔时间:({click_interval:.4f}s)")

# 切换点击循环状态的函数(修改后)
def toggle_click_loop():
    global click_loop_enabled, current_loop
    if click_loop_enabled:
        click_loop_enabled = False
        print("点击循环已停止")
        # 如果当前有循环线程在运行,尝试停止它(虽然daemon=True会在主程序退出时自动清理)
        if current_loop and current_loop.is_alive():
            current_loop.join(timeout=1)  # 等待线程结束,最多等待1秒
        # 重新显示主窗口,让用户可以选择新的功能
        root.deiconify()
    else:
        if current_choice:
            click_loop_enabled = True
            print("点击循环已启动")
            # 根据当前选择启动相应的循环线程
            if current_choice == "治疗":
                current_loop = threading.Thread(target=click_loop, args=(x1_base, y1_base), daemon=True)
                current_loop = threading.Thread(target=click_loop, args=(x2_base, y2_base), daemon=True)
                current_loop = threading.Thread(target=click_loop, args=(x2_base, y2_base), daemon=True)
 
            elif current_choice == "协助":
                current_loop = threading.Thread(target=click_loop, args=(x3_base, y3_base), daemon=True)
            current_loop.start()

# 定义用户选择功能的函数
def choose_function(selected_choice):
    global current_choice
    current_choice = selected_choice
    print(f"你选择了: {selected_choice}")
    root.withdraw()  # 隐藏主窗口
    # 不立即启动循环,等待用户按下ESC键

# 当窗口关闭时执行的函数
def on_closing():
    global click_loop_enabled
    click_loop_enabled = False  # 禁用点击循环
    root.destroy()  # 销毁主窗口

root = tk.Tk()
root.title("模拟点击")
root.geometry('240x100')
# 创建一个自定义字体
custom_font = font.Font(size=12, weight="bold")  # 您可以根据需要更改字体和大小
root.configure(bg='#9F9F9F')  # 使用颜色名称设置背景色
label = tk.Label(root, text="先选择模式\n再按esc开启或关闭", font=custom_font, bg='#8F8F8F')
label.pack(pady=5)
root.protocol("WM_DELETE_WINDOW", on_closing)  # 处理窗口关闭事件

# 创建并放置选择按钮
tk.Button(root, text="治疗", font=custom_font, width=10, height=6, command=lambda: choose_function("治疗"), bg='#2F6F2F', fg='#6F2F2F').pack(side=tk.LEFT, padx=10, pady=10)
tk.Button(root, text="协助", font=custom_font, width=10, height=6, command=lambda: choose_function("协助"), bg='#2F2F6F', fg='#2F6F2F').pack(side=tk.RIGHT, padx=10, pady=10)

# 使用keyboard库监听Esc键按下事件
keyboard.add_hotkey('esc', toggle_click_loop)

# 运行主事件循环
root.mainloop()
 
-------------------------附件config.ini-------------------------
[Coordinates]
x1_base = 370
y1_base = 2040
x2_base = 840
y2_base = 2040
x3_base = 610
y3_base = 2700
x_range = 80
y_range = 20
 
posted @ 2024-12-06 10:18  abs8023  阅读(135)  评论(0)    收藏  举报