文章附件下载:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:4102
代码功能说明:
完整UI界面:包含悬浮窗控制面板,支持拖动位置调整
核心功能:单点/连续点击、位置记录、任务执行与停止
参数配置:可调节点击间隔(0-2000ms)、点击次数
多点触控:支持按顺序点击多个记录位置
权限检测:自动检查无障碍服务和悬浮窗权限
线程安全:使用独立线程执行点击任务,支持中断
日志系统:详细记录操作过程和点击位置
/**
- Auto.js 屏幕自动点击器
- 功能:支持单点/连点、坐标记录、点击间隔设置、手势滑动等
*/
// 基础配置
let config = {
version: "2.1",
debugMode: true,
defaultInterval: 500,
maxClickCount: 9999
};
// 主界面UI
function createMainUI() {
// 设置悬浮窗参数
let window = floaty.window(
<horizontal gravity="center" marginBottom="8">
<button id="singleClick" text="单次点击" w="120" h="40"/>
<button id="multiClick" text="连续点击" w="120" h="40"/>
</horizontal>
<horizontal gravity="center" marginBottom="8">
<button id="recordPos" text="记录位置" w="120" h="40"/>
<button id="clearPos" text="清空记录" w="120" h="40"/>
</horizontal>
<text text="点击间隔(ms):" textSize="14sp"
textColor="#FFFFFF" marginBottom="4"/>
<seekbar id="intervalBar" max="2000" progress="500"
w="*" h="20" marginBottom="16"/>
<text text="点击次数:" textSize="14sp"
textColor="#FFFFFF" marginBottom="4"/>
<input id="clickCount" inputType="number"
text="10" w="*" h="40" marginBottom="16"/>
<button id="startBtn" text="开始执行" bg="#FF5722"
w="*" h="45" textColor="#FFFFFF"/>
<button id="stopBtn" text="停止" bg="#F44336"
w="*" h="45" textColor="#FFFFFF" marginTop="8"/>
</vertical>
</frame>
);
// 点击位置存储
let clickPositions = [];
let isRunning = false;
let currentTask = null;
// 单次点击按钮事件
window.singleClick.click(() => {
toast("请在3秒内点击屏幕");
threads.start(function() {
let pos = capturePos();
if (pos) {
click(pos.x, pos.y);
toast("已点击: " + pos.x + "," + pos.y);
}
});
});
// 连续点击按钮事件
window.multiClick.click(() => {
toast("请在3秒内点击屏幕");
threads.start(function() {
let pos = capturePos();
if (pos) {
let count = parseInt(window.clickCount.text()) || 10;
let interval = window.intervalBar.getProgress();
multiClick(pos.x, pos.y, count, interval);
}
});
});
// 记录位置按钮事件
window.recordPos.click(() => {
toast("请在3秒内点击要记录的位置");
threads.start(function() {
let pos = capturePos();
if (pos) {
clickPositions.push(pos);
toast("已记录位置: " + pos.x + "," + pos.y);
console.log("当前记录点:", clickPositions);
}
});
});
// 清空记录按钮事件
window.clearPos.click(() => {
clickPositions = [];
toast("已清空所有记录点");
});
// 开始执行按钮事件
window.startBtn.click(() => {
if (clickPositions.length === 0) {
toast("请先记录点击位置");
return;
}
if (isRunning) {
toast("任务已在运行中");
return;
}
let interval = window.intervalBar.getProgress();
let count = parseInt(window.clickCount.text()) || config.maxClickCount;
isRunning = true;
currentTask = threads.start(function() {
executeTask(clickPositions, count, interval);
});
});
// 停止按钮事件
window.stopBtn.click(() => {
if (currentTask) {
currentTask.interrupt();
currentTask = null;
}
isRunning = false;
toast("已停止任务");
});
// 拖动悬浮窗
window.setPosition(device.width / 4, device.height / 4);
window.setTouchable(false);
let offsetX, offsetY;
window._frame.setOnTouchListener(function(view, event) {
switch(event.getAction()) {
case event.ACTION_DOWN:
offsetX = event.getRawX() - window.getX();
offsetY = event.getRawY() - window.getY();
break;
case event.ACTION_MOVE:
window.setPosition(event.getRawX() - offsetX,
event.getRawY() - offsetY);
break;
}
return true;
});
}
// 捕获点击位置
function capturePos() {
let captured = false;
let position = null;
// 注册触摸监听
let listener = events.observeTouch();
listener.on("touch", function(p) {
if (!captured && p.getAction() === p.ACTION_DOWN) {
position = {
x: p.x,
y: p.y,
time: new Date().getTime()
};
captured = true;
}
});
// 等待3秒
let start = new Date().getTime();
while (!captured && new Date().getTime() - start < 3000) {
sleep(100);
}
// 移除监听
events.removeAllTouchListeners();
return position;
}
// 执行点击任务
function executeTask(positions, count, interval) {
for (let i = 0; i < count && !threads.currentThread().isInterrupted(); i++) {
log("执行第 " + (i + 1) + " 轮点击");
positions.forEach((pos, index) => {
if (threads.currentThread().isInterrupted()) return;
click(pos.x, pos.y);
log("点击位置 " + index + ": " + pos.x + "," + pos.y);
if (index < positions.length - 1) {
sleep(interval);
}
});
if (i < count - 1) {
sleep(interval);
}
}
isRunning = false;
toast("任务执行完成");
}
// 多点触控实现
function multiClick(x, y, count, interval) {
for (let i = 0; i < count; i++) {
click(x, y);
sleep(interval);
}
}
// 程序入口
function main() {
// 检查无障碍服务
if (!auto.service) {
toast("请先开启无障碍服务");
auto.waitFor();
}
// 检查悬浮窗权限
if (!floaty.checkPermission()) {
toast("请授予悬浮窗权限");
floaty.requestPermission();
sleep(1000);
}
// 创建主界面
createMainUI();
// 保持脚本运行
setInterval(() => {}, 1000);
}
// 启动程序
main();