繁体   English   中英

无法让鼠标输入点击

[英]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.

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