C++#include <Windows.h>
#include <conio.h>
#include <vector>
#include <string>
#include <thread>
#include <chrono>
#include <atomic>
#include <map>
#include <cstdio>
#include <algorithm>
using namespace std;
using namespace chrono;
// 全局控制变量
atomic_bool g_running(false);
atomic_bool g_paused(false);
atomic_bool g_selecting_position(false);
POINT g_selected_position = {0, 0};
// 高精度计时器类
class HighPrecisionTimer {
public:
HighPrecisionTimer() : start_time(high_resolution_clock::now()) {}
void reset() { start_time = high_resolution_clock::now(); }
double elapsed_ms() const {
return duration_cast<duration<double, milli>>(high_resolution_clock::now() - start_time).count();
}
private:
high_resolution_clock::time_point start_time;
};
// 隐藏/显示光标
void set_cursor(bool visible) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(handle, &cursorInfo);
cursorInfo.bVisible = visible;
SetConsoleCursorInfo(handle, &cursorInfo);
}
// 设置控制台窗口位置和大小
void set_console_window() {
HWND console = GetConsoleWindow();
SetWindowPos(console, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
system("mode con cols=60 lines=20");
}
// 显示菜单
void show_menu() {
system("cls");
printf("=== 安全连点器 ===\n\n");
printf("当前状态: %s\n", g_running ? (g_paused ? "已暂停" : "运行中") : "已停止");
printf("\n请选择功能:\n");
printf(" 1. 连续按键输入\n");
printf(" 2. 连续鼠标点击\n");
printf(" 3. 连续滚轮滚动\n");
printf(" 4. 组合模式(按键+鼠标)\n");
printf(" 0. 退出\n");
}
// 获取用户自定义按键
vector<WORD> get_custom_keys() {
vector<WORD> keys;
char input[256] = {0};
system("cls");
printf("=== 自定义按键设置 ===\n\n");
printf("请输入要模拟的按键序列(例如: ABC123或F1F2F3)\n");
printf("支持字母(A-Z)、数字(0-9)、功能键(F1-F12)、特殊键(空格/Tab/回车/ESC)\n");
printf("方向键: 上<u> 下<d> 左<l> 右<r>\n");
printf("输入完成后按Enter键确认:\n");
printf("> ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = 0;
for (int i = 0; input[i] != '\0'; i++) {
WORD key = 0;
// 检查方向键符号
if (input[i] == '<') {
if (input[i+1] == 'u' && input[i+2] == '>') {
key = VK_UP;
i += 2; // 跳过<u>中的u>
}
else if (input[i+1] == 'd' && input[i+2] == '>') {
key = VK_DOWN;
i += 2; // 跳过<d>中的d>
}
else if (input[i+1] == 'l' && input[i+2] == '>') {
key = VK_LEFT;
i += 2; // 跳过<l>中的l>
}
else if (input[i+1] == 'r' && input[i+2] == '>') {
key = VK_RIGHT;
i += 2; // 跳过<r>中的r>
}
}
else if (toupper(input[i]) == 'F') {
// 处理功能键 F1-F12
if (isdigit(input[i+1])) {
int fn = input[i+1] - '0';
if (i+2 < strlen(input) && isdigit(input[i+2])) {
fn = fn * 10 + (input[i+2] - '0');
i++; // 跳过第二个数字字符
}
i++; // 跳过第一个数字字符
if (fn >= 1 && fn <= 12) {
key = VK_F1 + (fn - 1);
}
}
}
else if (isalpha(input[i])) {
key = toupper(input[i]);
}
else if (isdigit(input[i])) {
key = input[i];
}
else if (input[i] == ' ') {
key = VK_SPACE;
}
else if (input[i] == '\t') {
key = VK_TAB;
}
else if (input[i] == '\n' || input[i] == '\r') {
key = VK_RETURN;
}
else if (input[i] == 27) {
key = VK_ESCAPE;
}
if (key != 0) {
keys.push_back(key);
i++; // 移动到下一个字符
} else {
i++; // 无效字符,跳过
}
}
if (keys.empty()) {
printf("\n未输入有效按键,使用默认按键(A-Z,0-9)\n");
this_thread::sleep_for(chrono::milliseconds(1000));
for (char c = 'A'; c <= 'Z'; c++) keys.push_back(c);
for (char c = '0'; c <= '9'; c++) keys.push_back(c);
keys.push_back(VK_SPACE);
keys.push_back(VK_RETURN);
keys.push_back(VK_TAB);
keys.push_back(VK_ESCAPE);
// 添加方向键到默认按键
keys.push_back(VK_UP);
keys.push_back(VK_DOWN);
keys.push_back(VK_LEFT);
keys.push_back(VK_RIGHT);
}
return keys;
}
// 选择点击位置
void select_click_position() {
system("cls");
printf("=== 选择点击位置 ===\n\n");
printf("请将鼠标移动到目标位置后按空格键确认\n");
printf("按ESC键取消\n");
g_selecting_position = true;
POINT pt;
while (g_selecting_position) {
if (GetCursorPos(&pt)) {
printf("当前位置: X=%d, Y=%d (按空格确认)\r", pt.x, pt.y);
}
if (_kbhit()) {
int ch = _getch();
if (ch == 32) { // 空格键确认
g_selecting_position = false;
g_selected_position = pt;
printf("\n已选择位置: X=%d, Y=%d\n", pt.x, pt.y);
this_thread::sleep_for(chrono::milliseconds(1000));
} else if (ch == 27) { // ESC键取消
g_selecting_position = false;
g_selected_position = {0, 0};
printf("\n已取消选择\n");
this_thread::sleep_for(chrono::milliseconds(1000));
}
}
this_thread::sleep_for(chrono::milliseconds(50));
}
}
// 模拟键盘输入
void simulate_keyboard(int actions_per_sec, const vector<WORD>& keys) {
const double min_interval_ms = 1.0;
double target_interval_ms = max(1000.0 / actions_per_sec, min_interval_ms);
int batch_size = max(1, min(100, (int)(16.0 / target_interval_ms)));
HighPrecisionTimer timer;
double accumulated_error = 0.0;
double last_time = 0.0;
while (g_running) {
if (g_paused) {
this_thread::sleep_for(chrono::milliseconds(100));
continue;
}
double current_time = timer.elapsed_ms();
double elapsed = current_time - last_time;
if (elapsed >= target_interval_ms - accumulated_error) {
accumulated_error = max(0.0, (elapsed - target_interval_ms));
for (int i = 0; i < batch_size && g_running && !g_paused; ++i) {
for (WORD key : keys) {
if (!g_running) break;
INPUT input[2] = {0};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = key;
input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = key;
input[1].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));
}
}
last_time = current_time;
}
if (target_interval_ms > 2.0) {
this_thread::sleep_for(chrono::milliseconds(1));
} else {
this_thread::yield();
}
}
}
// 安全固定位置点击 (改进版)
void safe_fixed_position_click(int actions_per_sec, bool left, bool right) {
const double min_interval_ms = 1.0;
double target_interval_ms = max(1000.0 / actions_per_sec, min_interval_ms);
int batch_size = max(1, min(100, (int)(16.0 / target_interval_ms)));
HighPrecisionTimer timer;
double accumulated_error = 0.0;
double last_time = 0.0;
while (g_running) {
if (g_paused) {
this_thread::sleep_for(chrono::milliseconds(100));
continue;
}
double current_time = timer.elapsed_ms();
double elapsed = current_time - last_time;
if (elapsed >= target_interval_ms - accumulated_error) {
accumulated_error = max(0.0, (elapsed - target_interval_ms));
// 保存当前鼠标位置
POINT original_pos;
GetCursorPos(&original_pos);
// 移动到目标位置并点击
for (int i = 0; i < batch_size && g_running && !g_paused; ++i) {
if (left) {
SetCursorPos(g_selected_position.x, g_selected_position.y);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
if (right) {
SetCursorPos(g_selected_position.x, g_selected_position.y);
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
}
}
// 恢复鼠标位置
SetCursorPos(original_pos.x, original_pos.y);
last_time = current_time;
}
if (target_interval_ms > 2.0) {
this_thread::sleep_for(chrono::milliseconds(1));
} else {
this_thread::yield();
}
}
}
// 跟随鼠标点击
void follow_cursor_click(int actions_per_sec, bool left, bool right) {
const double min_interval_ms = 1.0;
double target_interval_ms = max(1000.0 / actions_per_sec, min_interval_ms);
int batch_size = max(1, min(100, (int)(16.0 / target_interval_ms)));
HighPrecisionTimer timer;
double accumulated_error = 0.0;
double last_time = 0.0;
while (g_running) {
if (g_paused) {
this_thread::sleep_for(chrono::milliseconds(100));
continue;
}
double current_time = timer.elapsed_ms();
double elapsed = current_time - last_time;
if (elapsed >= target_interval_ms - accumulated_error) {
accumulated_error = max(0.0, (elapsed - target_interval_ms));
for (int i = 0; i < batch_size && g_running && !g_paused; ++i) {
if (left) {
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
if (right) {
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
}
}
last_time = current_time;
}
if (target_interval_ms > 2.0) {
this_thread::sleep_for(chrono::milliseconds(1));
} else {
this_thread::yield();
}
}
}
// 模拟滚轮滚动
void simulate_mouse_wheel(int scrolls_per_sec, int scroll_amount) {
const double min_interval_ms = 10.0;
double target_interval_ms = max(1000.0 / scrolls_per_sec, min_interval_ms);
HighPrecisionTimer timer;
double accumulated_error = 0.0;
double last_time = 0.0;
while (g_running) {
if (g_paused) {
this_thread::sleep_for(chrono::milliseconds(100));
continue;
}
double current_time = timer.elapsed_ms();
double elapsed = current_time - last_time;
if (elapsed >= target_interval_ms - accumulated_error) {
accumulated_error = max(0.0, (elapsed - target_interval_ms));
INPUT input = {0};
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_WHEEL;
input.mi.mouseData = scroll_amount * WHEEL_DELTA / 120;
SendInput(1, &input, sizeof(INPUT));
this_thread::sleep_for(chrono::milliseconds(5));
last_time = current_time;
}
if (target_interval_ms > 2.0) {
this_thread::sleep_for(chrono::milliseconds(1));
} else {
this_thread::yield();
}
}
}
// 主控制函数
void main_control() {
set_console_window();
set_cursor(true);
while (true) {
show_menu();
int choice = _getch();
while (choice < '0' || choice > '4') {
choice = _getch();
}
if (choice == '0') {
g_running = false;
break;
}
// 配置参数
int actions_per_sec = 10;
bool left_click = true;
bool right_click = false;
int scroll_amount = 100;
bool fixed_position = false;
char input[10] = {0};
vector<WORD> custom_keys;
system("cls");
printf("配置参数 (按Enter使用默认值):\n");
if (choice == '1' || choice == '4') {
custom_keys = get_custom_keys();
printf("\n每秒操作次数(默认10, 最大1000): ");
fgets(input, sizeof(input), stdin);
if (strlen(input) > 0) actions_per_sec = atoi(input);
actions_per_sec = max(1, min(1000, actions_per_sec));
}
if (choice == '2' || choice == '4') {
printf("点击模式:\n");
printf(" 1. 固定位置点击\n");
printf(" 2. 跟随鼠标点击\n");
int mode = _getch();
while (mode < '1' || mode > '2') {
mode = _getch();
}
fixed_position = (mode == '1');
if (fixed_position) {
select_click_position();
if (g_selected_position.x == 0 && g_selected_position.y == 0) {
printf("未选择位置,默认使用跟随鼠标模式\n");
fixed_position = false;
this_thread::sleep_for(chrono::milliseconds(1000));
}
}
printf("\n左键点击 (Y/N, 默认Y): ");
left_click = (toupper(_getch()) != 'N');
printf("\n右键点击 (Y/N, 默认N): ");
right_click = (toupper(_getch()) == 'Y');
printf("\n每秒点击次数(默认10, 最大1000): ");
fgets(input, sizeof(input), stdin);
if (strlen(input) > 0) actions_per_sec = atoi(input);
actions_per_sec = max(1, min(1000, actions_per_sec));
}
if (choice == '3') {
printf("滚轮滚动量(默认100, 正数向上,负数向下): ");
fgets(input, sizeof(input), stdin);
if (strlen(input) > 0) scroll_amount = atoi(input);
printf("\n每秒滚动次数(默认10, 最大500): ");
fgets(input, sizeof(input), stdin);
if (strlen(input) > 0) actions_per_sec = atoi(input);
actions_per_sec = max(1, min(500, actions_per_sec));
}
// 启动线程
g_running = true;
g_paused = false;
vector<thread> threads;
if (choice == '1') {
threads.emplace_back(simulate_keyboard, actions_per_sec, custom_keys);
}
else if (choice == '2') {
if (fixed_position) {
threads.emplace_back(safe_fixed_position_click, actions_per_sec, left_click, right_click);
} else {
threads.emplace_back(follow_cursor_click, actions_per_sec, left_click, right_click);
}
}
else if (choice == '3') {
threads.emplace_back(simulate_mouse_wheel, actions_per_sec, scroll_amount);
}
else if (choice == '4') {
threads.emplace_back(simulate_keyboard, actions_per_sec, custom_keys);
if (fixed_position) {
threads.emplace_back(safe_fixed_position_click, actions_per_sec, left_click, right_click);
} else {
threads.emplace_back(follow_cursor_click, actions_per_sec, left_click, right_click);
}
}
// 控制循环
set_cursor(false);
while (g_running) {
if (_kbhit()) {
int ch = _getch();
if (ch == 27) {
g_running = false;
} else if (ch == ' ') {
g_paused = !g_paused;
}
}
system("cls");
printf("=== 正在运行 ===\n\n");
printf("状态: %s\n", g_paused ? "已暂停" : "运行中");
printf("模式: %s\n",
choice == '1' ? "按键输入" :
choice == '2' ? (fixed_position ? "固定位置点击" : "跟随鼠标点击") :
choice == '3' ? "滚轮滚动" : "组合模式(按键+鼠标)");
printf("速度: 每秒%d次\n", actions_per_sec);
if (choice == '2' || choice == '4') {
printf("点击: %s%s\n",
left_click ? "左键 " : "",
right_click ? "右键" : "");
if (fixed_position) {
printf("位置: X=%d, Y=%d\n", g_selected_position.x, g_selected_position.y);
}
}
if (choice == '3') printf("滚动量: %d\n", scroll_amount);
printf("\n操作:\n");
printf(" 空格键 - 暂停/继续\n");
printf(" ESC键 - 停止\n");
this_thread::sleep_for(chrono::milliseconds(50));
}
for (auto& t : threads) {
if (t.joinable()) t.join();
}
printf("\n操作已停止,按任意键返回菜单...");
_getch();
}
}
int main() {
main_control();
return 0;
}