簡體   English   中英

(Windows C++) 如何在 FPS 游戲中模擬鼠標移動?

[英](Windows C++) How to simulate mouse movement in FPS games?

我正在嘗試模擬 FPS 游戲中的鼠標移動,更具體地說是Valorant 我知道SetCursorPos() function 和mouse_event() function,它們都可以很好地更改光標的 Z4757FE07FD492A8BE0EA6A760D683D6E。 這適用於使用不斷居中 cursor 技術的 FPS 游戲,但Valorant似乎沒有這樣做。 我編寫了一個程序來不斷檢查我的光標的 position (使用GetCursorPos() )和我的 cursor 從來沒有居中,如果我將鼠標移動到一個角落然后繼續移動它,我的角色會繼續旋轉。 那么,當我的光標的 position 沒有改變時, Valorant是如何感知我正在移動鼠標的,我該如何應對這種情況並在Valorant中模擬鼠標移動?

順便說一句,別擔心——我不是想作弊,只是想在 freecam 中為電影鏡頭做出流暢的動作。

我不確定 Valorant 是如何實現的,但是當光標的 position 沒有改變時,我可以使用RegisterRawInputDevices來獲取鼠標事件:

#include <windows.h>
#include <iostream>

using namespace std;
BOOL registerTouchpadForInput(HWND hWnd)
{
    RAWINPUTDEVICE rid;
    rid.dwFlags = RIDEV_NOLEGACY | RIDEV_INPUTSINK;
    rid.usUsagePage = 1;                            
    rid.usUsage = 2;
    rid.hwndTarget = hWnd;
    return RegisterRawInputDevices(&rid, 1, sizeof(rid));
}

static LRESULT CALLBACK NVTouch_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    BOOL  registrationStatus = false;
    switch (message)
    {
    case WM_CREATE:
        registrationStatus = registerTouchpadForInput(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_INPUT:
        printf("WM_INPUT ");
        return DefWindowProc(hwnd, message, wParam, lParam);
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

int main()
{
    WNDCLASSEX wndclass = {
        sizeof(WNDCLASSEX),
        CS_DBLCLKS,
        NVTouch_WindowProc,
        0,
        0,
        GetModuleHandle(0),
        LoadIcon(0,IDI_APPLICATION),
        LoadCursor(0,IDC_ARROW),
        HBRUSH(COLOR_WINDOW + 1),
        0,
        L"myclass",
        LoadIcon(0,IDI_APPLICATION)
    };
    bool isClassRegistered = false;
    isClassRegistered = RegisterClassEx(&wndclass);
    if (isClassRegistered)
    {
        HWND window = CreateWindow(wndclass.lpszClassName, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, GetModuleHandle(0), NULL);
        if (window)
        {
            ShowWindow(window, SW_SHOWDEFAULT);
            MSG msg;
            while (GetMessage(&msg, 0, 0, 0))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    }
}

然后,使用SendInput模擬鼠標在屏幕邊緣的相對移動:

INPUT buffer;
ZeroMemory(&buffer, sizeof(buffer));
buffer.type = INPUT_MOUSE;
buffer.mi.dx = 10;
buffer.mi.dy = 10;
buffer.mi.mouseData = 0;
buffer.mi.dwFlags = MOUSEEVENTF_MOVE;
buffer.mi.time = 0;
buffer.mi.dwExtraInfo = 0;
while (1)
{
    Sleep(1000);
    SendInput(1, &buffer, sizeof(INPUT));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM