程序在对mouse_event的 Windows API 调用中冻结

Program is freezing on Windows API call to mouse_event

本文关键字:API Windows 调用 冻结 event mouse 程序      更新时间:2023-10-16

我正在尝试编写一个自动点击器机器人,但我不知道为什么程序在我运行时会阻塞。当程序进入cliquer()时会发生这种情况。

是的,我是一名学生,所以我是一个初学者,如果你问的话,我确实是法国人。

谢谢你给我一个答案。

#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;
void cliquer();
int main()
{
time_t time_to_run;
time_t ac_time;
time_t time_left;
time(&ac_time);                                                     // Actualiser le temps
cout << "Hello, wanna cheat on Voltaire ?" << endl;
cout << "For how much time do you want to run it (s) ?" << endl;
cin >> time_to_run;
time(&ac_time);                                                     // Actualiser le temps
time_to_run = ac_time + time_to_run;                                // Le temps à executer =  temps actuel + temps demandé
while(ac_time < time_to_run)
{
time(&ac_time);                                                 // Actualiser le temps
time_left = time_to_run - ac_time;

//if(time_left <= 18000)                                          // Si supérieur à 5min
//{
cout << "TIME LEFT : " << time_left << endl;
//}
Sleep(100);
cliquer();
}
return 0;
}
//---------------------------------------------------------------------------------------------------------------------------------------
void cliquer()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 957, 396, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 957, 396 , 0, 0);
}

冻结的原因是您的鼠标焦点位于控制台中。您可以添加ShowWindow(GetConsoleWindow(), SW_MINIMIZE)以最小化控制台,以避免冻结。

mouse_event已经过时了。请使用发送输入。

这是您可以参考的示例:

#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;
time_t time_to_run;
time_t ac_time;
time_t time_left;
HWND hwnd;
void autoclick()
{
INPUT input[2];
ZeroMemory(input, sizeof(INPUT) * 2);
input[0].mi.dx = input[1].mi.dx = 945;
input[0].mi.dy = input[1].mi.dy = 500;
input[0].type = input[1].type = INPUT_MOUSE;
input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
time(&ac_time);
time_to_run = ac_time + time_to_run;
while (ac_time < time_to_run)
{
time(&ac_time);                                                 // Actualiser le temps
time_left = time_to_run - ac_time;
cout << "TIME LEFT : " << time_left << "s"<<endl;
SendInput(2, input, sizeof(INPUT));
Sleep(100);
}
}
int main()
{
hwnd = GetConsoleWindow();
cout << "Hello, wanna cheat on Voltaire ?" << endl;
while (1)
{
ShowWindow(hwnd, SW_RESTORE);
cout << "For how much time do you want to run it (s) ?" << endl;
cin >> time_to_run;
ShowWindow(hwnd, SW_MINIMIZE);
autoclick();
}
return 0;
}