跳到正文

科技·工程

C++ 鼠标连点器

cin_future约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年10月5日
收录
2026年10月5日
更新
2026年11月18日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

编译运行后长按Shift开始连点,esc退出程序

源码:

C++
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif

#include <windows.h>
#include <iostream>
#include <atomic>
#include <thread>

std::atomic<bool> g_running{true};

void simulateLeftClick() {
    INPUT input = {};
    input.type = INPUT_MOUSE;

    input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    SendInput(1, &input, sizeof(INPUT));

    Sleep(1);

    input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(1, &input, sizeof(INPUT));
}

void clickLoop(int intervalMs) {
    while (g_running.load()) {
        if (GetAsyncKeyState(VK_SHIFT) & 0x8000) {
            simulateLeftClick();
            Sleep(intervalMs);
        } else {
            Sleep(10);
        }
    }
}

int main() {
    SetConsoleTitleA("Mouse Auto Clicker");

    std::cout << "=== C++ 鼠标连点器 ===" << std::endl;
    std::cout << "按住 Shift 开始连点,松开停止" << std::endl;
    std::cout << "按 Esc 退出程序" << std::endl;

    int intervalMs = 50;
    std::cout << "当前点击间隔: " << intervalMs << " ms" << std::endl;

    std::thread worker(clickLoop, intervalMs);

    while (g_running.load()) {
        if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
            g_running = false;
        }
        Sleep(30);
    }

    if (worker.joinable()) {
        worker.join();
    }

    std::cout << "程序已退出。" << std::endl;
    return 0;
}

注意:Dev-C++未开启C++11支持的(即编译运行后报错),按以下步骤打开:

1.打开菜单:工具 → 编译选项

2.勾选:编译时加入以下命令

3.在下面输入框里填:

代码
-std=c++11 -luser32

4.再勾选:在连接器命令行加入以下命令

5.如果下面也有输入框,同样填步骤3的文本

6.点确定

7.重新编译运行

原文