[英]can't get MouseInput to click
一直在嘗試制作一個基本的自動點擊器,但似乎無法讓 MouseInput 來點擊鼠標。 我相信這可能是導致此問題的簡單原因。 沒有錯誤發生,它只是不會做它打算做的事情。
有人可以看看它並告訴我它有什么問題嗎?
#include <iostream>
#include <Windows.h>
#include <thread>
#include <conio.h>
void loop()
{
int cps;
std::cout << "Enter desired clicks per second: ";
std::cin >> cps;
bool toggled = false;
while (true)
{
static bool bPressedToggle = false;
if (GetKeyState(VK_TAB) < 0)
bPressedToggle = true;
else if (bPressedToggle)
{
bPressedToggle = false;
toggled = !toggled;
std::cout << (toggled ? "Toggled" : "Disabled") << std::endl;
if (!toggled) continue;
}
if (toggled && (GetAsyncKeyState(VK_LBUTTON) & (1 << 16)))
{
POINT pntCurrentCursor;
GetCursorPos(&pntCurrentCursor);
INPUT inpMouseInput;
inpMouseInput.type = INPUT_MOUSE;
inpMouseInput.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
inpMouseInput.mi.dx = pntCurrentCursor.x;
inpMouseInput.mi.dy = pntCurrentCursor.y;
SendInput(1, &inpMouseInput, sizeof(INPUT)); // DOWN
RtlZeroMemory(&inpMouseInput, sizeof(INPUT));
inpMouseInput.type = INPUT_MOUSE;
inpMouseInput.mi.dwFlags = MOUSEEVENTF_LEFTUP;
inpMouseInput.mi.dx = pntCurrentCursor.x;
inpMouseInput.mi.dy = pntCurrentCursor.y;
SendInput(1, &inpMouseInput, sizeof(INPUT)); // UP
Sleep( 1000 / cps);
}
//Sleep(1);
}
}
int main()
{
loop();
return 0;
} ```
作為補充,需要將GetCursorPos
返回的屏幕坐標轉換為絕對坐標並傳遞給SendInput
:
dx = (x * 65536) / GetSystemMetrics(SystemMetric.SM_CXSCREEN);
dy = (y * 65536) / GetSystemMetrics(SystemMetric.SM_CYSCREEN);
並且@IInspectable 提供了足夠有用的信息,為此我創建了一個簡單的演示僅供參考。
#include <iostream>
#include <Windows.h>
#include <thread>
#include <conio.h>
#define IDT_MOUSETRAP 100
HWND hWnd;
bool toggled = false;
int cps;
HANDLE hThread;
BOOL flag = true;
VOID CALLBACK MyTimerProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
{
POINT pntCurrentCursor;
GetCursorPos(&pntCurrentCursor);
INPUT inpMouseInput;
inpMouseInput.type = INPUT_MOUSE;
inpMouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN;
inpMouseInput.mi.dx = (pntCurrentCursor.x * 65536) / GetSystemMetrics(SM_CXSCREEN);;
inpMouseInput.mi.dy = (pntCurrentCursor.y * 65536) / GetSystemMetrics(SM_CYSCREEN);;
SendInput(1, &inpMouseInput, sizeof(INPUT)); // DOWN
RtlZeroMemory(&inpMouseInput, sizeof(INPUT));
inpMouseInput.type = INPUT_MOUSE;
inpMouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP;
inpMouseInput.mi.dx = pntCurrentCursor.x;
inpMouseInput.mi.dy = pntCurrentCursor.y;
SendInput(1, &inpMouseInput, sizeof(INPUT)); // UP
}
DWORD WINAPI MyThreadFunction(LPVOID lpParam)
{
UINT uResult = SetTimer(hWnd, IDT_MOUSETRAP, 1000 / cps, (TIMERPROC)MyTimerProc);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static bool bPressedToggle = false;
DWORD ThreadId;
switch (message)
{
case WM_CREATE:
{
std::cout << "Enter desired clicks per second: ";
std::cin >> cps;
}
break;
case WM_KEYDOWN:
{
if (GetKeyState(VK_TAB) < 0)
{
bPressedToggle = true;
if (bPressedToggle)
{
bPressedToggle = false;
toggled = !toggled;
std::cout << (toggled ? "Toggled" : "Disabled") << std::endl;
}
if (toggled == true && flag == true)
{
flag = false;
hThread = CreateThread(NULL, 0, MyThreadFunction, NULL, 0, &ThreadId);
}
else if (toggled == false && flag == false)
{
KillTimer(hWnd, IDT_MOUSETRAP);
flag = true;
}
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void Createyourwindow()
{
WNDCLASSEXW wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = GetModuleHandle(NULL);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = L"MyClass";
RegisterClassExW(&wcex);
hWnd = CreateWindowW(L"MyClass", L"window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, GetModuleHandle(NULL), NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
MSG msg;
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
int main()
{
Createyourwindow();
return 0;
}
注意:因為需要窗口的消息循環,所以按TAB鍵時鼠標焦點需要在窗口內。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.