【问题标题】:C++ mouse functions not responding, Trying to create a clicking simulatorC++ 鼠标功能没有响应,试图创建一个点击模拟器
【发布时间】:2020-08-13 16:56:01
【问题描述】:
#include <Windows.h>
#include <iostream>
#include <conio.h>

//void MouseRetracer(POINT mousePos, int noOfSaves, int delay)
//{
//  
//}

int main(int argc, char* argv[])
{
    std::cout << "Enter delay between 2 clicks... ";
    int delay;
    std::cin >> delay;
    std::cout << "Hover the mouse to the desired location and press 'k'...\n";
    //std::cin.get();
    std::cout << "Press ENTER to start taking inputs!!!" << std::endl;

    bool quit = true;
    int noOfSaves = 0;
    POINT mousePos[100];

    while (quit)
    {
        if (_kbhit())
        {
            char kbKey = _getch();
            if (kbKey == 'k')
            {
                GetCursorPos(&mousePos[noOfSaves]);
                std::cout << noOfSaves + 1 << " positions saved...\n";
                noOfSaves++;
            }
            if (kbKey == 'q')
                quit = false;
        }
    }

    std::cout << "DO NOT move mouse now!" << std::endl;
    //MouseRetracer(mousePos, noOfSaves, delay);
    for (int i = 0; i < noOfSaves; i++)
    {
        //std::cout << mousePos[i].x << '\t';
        Sleep(delay);

        mouse_event(MOUSEEVENTF_LEFTDOWN, mousePos[i].x, mousePos[i].y, NULL, NULL);
        mouse_event(MOUSEEVENTF_LEFTUP, mousePos[i].x, mousePos[i].y, NULL, NULL);

        std::cout << i + 1 << "th save clicked!\n";
    }

    /*std::cout << "Do this again? (y/n)\n";
    char repeat;
    std::cin >> repeat;

    switch (repeat)
    {
    case 'y':
        system("cls");
        break;
    case 'Y':
        system("cls");
        break;
    default:
        return 0;
    }*/

    return 0;
}

上面的代码可以正确地接受输入,即它创建POINT结构并正确保存数据mousePos[0]mousePos[1]等(我通过Visual Studio 2019调试器检查)但它不能模拟使用mouse_event()的点击.没有错误或警告。问题似乎是mouse_event(),即使它在使用POINT mousePos; 的其他程序中运行良好。使用POINT mousePos[100] 似乎会带来麻烦。

提前谢谢你。

【问题讨论】:

  • 请考虑使用可运行代码发布问题,以便人们更轻松地解决问题。否则,我们只是不断猜测您想要/需要什么。请检查how_to_ask
  • @dboy 你现在可以检查一下吗,我添加了完整的代码。
  • mouse_event: “此功能已被取代。请改用SendInput。”
  • 但它适用于使用POINT mousePos; 的其他程序。定义POINT mousePos[100] 似乎解决了问题。

标签: c++ c winapi visual-c++


【解决方案1】:

使用SetCursorPos 设置光标位置,然后使用mouse_event 发送鼠标按钮点击事件。以下是我的工作代码。你可以试一试。

for (int i = 0; i < noOfSaves; i++)
{
    Sleep(delay);

    SetCursorPos(mousePos[i].x, mousePos[i].y);

    mouse_event(MOUSEEVENTF_LEFTDOWN, mousePos[i].x, mousePos[i].y, NULL, NULL);
    mouse_event(MOUSEEVENTF_LEFTUP, mousePos[i].x, mousePos[i].y, NULL, NULL);

    std::cout << i + 1 << "th save clicked!\n";
}

注意: 建议使用SendInput 而不是mouse_event,因为该功能已被取代。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2014-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2019-12-30
    • 2012-12-08
    相关资源
    最近更新 更多