You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++新手求助:实现按住左键触发持续自动点击且循环不中断

Hey there! Let's tackle this auto-clicker issue you're having. As a C++ newbie diving into game automation, it's totally normal to run into hiccups with mouse input handling. Let's break down what's going wrong and fix it step by step.

What's Off with Your Current Code

First, let's look at the key issues in your implementation:

  • Unnecessary flags: Your dwFlags includes MOUSEEVENTF_ABSOLUTE and MOUSEEVENTF_MOVE, which you don't need here—since you're just clicking without moving the cursor, these flags can introduce unexpected behavior.
  • Incorrect GetAsyncKeyState mask: GetAsyncKeyState returns a 16-bit SHORT, so the correct mask to check if the key is held down is 0x8000 (the highest bit of a 16-bit value). Using 0x80000000 works in 32-bit builds but isn't proper practice.
  • Potential input interference: Simulating full click events (down + up) in one SendInput call can sometimes interfere with your GetAsyncKeyState check, causing the loop to exit prematurely.

Fixed Implementation

Here's a revised version of your code that addresses these issues and keeps the auto-click running smoothly while holding the left mouse button:

#include <windows.h>

int main() {
    int cps = 10; // Set your desired clicks per second here
    bool enabled = false;

    // Optional: Improve sleep precision for more consistent clicks
    timeBeginPeriod(1);

    while (true) {
        // Check if left mouse button is physically held down
        if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) {
            enabled = true;
            // Keep clicking as long as the button is held
            while (GetAsyncKeyState(VK_LBUTTON) & 0x8000) {
                INPUT input = {0};
                input.type = INPUT_MOUSE;

                // Simulate left mouse button press
                input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
                SendInput(1, &input, sizeof(INPUT));

                // Simulate left mouse button release (completes one click)
                input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
                SendInput(1, &input, sizeof(INPUT));

                // Wait for the next click based on CPS
                Sleep(1000 / cps);
            }
            enabled = false;
        }
        // Reduce CPU usage when idle
        Sleep(10);
    }

    timeEndPeriod(1);
    return 0;
}

Key Improvements Explained

  • Split click into two SendInput calls: Separating the LEFTDOWN and LEFTUP events makes the click action more explicit and reliable for game environments, which often expect distinct press/release signals.
  • Corrected state detection: Using 0x8000 as the mask for GetAsyncKeyState ensures we correctly detect the physical button hold state without false positives.
  • Reduced CPU usage: The small Sleep(10) in the outer loop prevents your program from hogging CPU resources when the auto-clicker isn't active.
  • Precision boost: timeBeginPeriod(1) improves the accuracy of Sleep, making your click timing more consistent (remember to pair it with timeEndPeriod(1) at exit).

Extra Tips

  • Add a toggle key (like F1) to enable/disable the auto-clicker without holding the left button: Check GetAsyncKeyState(VK_F1) & 0x8000 to flip the enabled state, then run the click loop only when enabled is true and the left button is held.
  • Note that some games have anti-cheat systems that may detect automated input—only use this in environments where it's allowed.

内容的提问来源于stack exchange,提问作者Taco Gamez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:44:11