简体   繁体   中英

can't get MouseInput to click

been trying to make a basic auto clicker and cannot seem to get MouseInput to click the mouse. I believe it's probably something simple causing this issue. No errors occur it just won't do what it is intended to do.

Could somebody take a look at it and tell me whats wrong with it?

#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;
} ```

As a supplement, the screen coordinates returned by GetCursorPos need to be converted to absolute and passed to SendInput :

dx = (x * 65536) / GetSystemMetrics(SystemMetric.SM_CXSCREEN);
dy = (y * 65536) / GetSystemMetrics(SystemMetric.SM_CYSCREEN);

And @IInspectable had provided enough useful information, for this I have created a simple demo for reference only.

#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;
}

Note: Because the message loop of the window is needed, the mouse focus needs to be inside the window when pressing the TAB key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM