My mouse polling rate is low so could you add extra function that will interpolating the mouse position between samples so its smoother in the code below without changing anything? Just use same mouse method that is already used in code since only this works for me.

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

// Function to simulate key press using scan codes
void PressKey(WORD scanCode) {
    INPUT input = {};
    input.type = INPUT_KEYBOARD;
    input.ki.wScan = scanCode;
    input.ki.dwFlags = KEYEVENTF_SCANCODE;
    SendInput(1, &input, sizeof(INPUT));
}

void ReleaseKey(WORD scanCode) {
    INPUT input = {};
    input.type = INPUT_KEYBOARD;
    input.ki.wScan = scanCode;
    input.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
    SendInput(1, &input, sizeof(INPUT));
}

// Helper class to manage key states
class KeyState {
public:
    std::atomic<bool> aPressed;
    std::atomic<bool> dPressed;

    KeyState() : aPressed(false), dPressed(false) {}

    void PressA() {
        if (!aPressed.load()) {
            PressKey(0x1E); // Scan code for 'A'
            aPressed = true;
        }
    }

    void ReleaseA() {
        if (aPressed.load()) {
            ReleaseKey(0x1E); // Scan code for 'A'
            aPressed = false;
        }
    }

    void PressD() {
        if (!dPressed.load()) {
            PressKey(0x20); // Scan code for 'D'
            dPressed = true;
        }
    }

    void ReleaseD() {
        if (dPressed.load()) {
            ReleaseKey(0x20); // Scan code for 'D'
            dPressed = false;
        }
    }

    void ReleaseAll() {
        ReleaseA();
        ReleaseD();
    }
};

// Function to bring Roblox window to foreground
HWND GetRobloxWindow() {
    HWND hwnd = FindWindow(NULL, NULL);
    while (hwnd != NULL) {
        wchar_t title[256];
        GetWindowText(hwnd, title, sizeof(title) / sizeof(wchar_t));
        std::wstring windowTitle(title);
        if (windowTitle.find(L"Roblox") != std::wstring::npos) {
            return hwnd;
        }
        hwnd = FindWindowEx(NULL, hwnd, NULL, NULL);
    }
    return NULL;
}

// Constants for thresholds and timings
const LONG MOVEMENT_THRESHOLD = 5; // Minimum pixels of movement before switching keys
const DWORD KEY_PRESS_DURATION = 10; // Minimum duration to hold a key in milliseconds

// Function to get the current time in milliseconds
DWORD GetCurrentTimeMs() {
    return GetTickCount();
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    static bool isRightButtonDown = false;
    static KeyState keyState;
    static HWND robloxHwnd = NULL;

    // Variables for cumulative mouse movement and timing
    static LONG cumulativeMouseX = 0;
    static DWORD lastSwitchTime = 0;

    switch (uMsg) {
    case WM_INPUT: {
        BYTE lpb[sizeof(RAWINPUT)];
        UINT dwSize = sizeof(lpb);

        if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize,
            sizeof(RAWINPUTHEADER)) != dwSize) {
            std::cerr << "GetRawInputData does not return correct size!" << std::endl;
            break;
        }

        RAWINPUT* raw = (RAWINPUT*)lpb;

        // Check if the right mouse button is held down
        if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) {
            if (!isRightButtonDown) {
                isRightButtonDown = true;
                if (!robloxHwnd) {
                    robloxHwnd = GetRobloxWindow();
                    if (robloxHwnd) {
                        SetForegroundWindow(robloxHwnd);
                    }
                    else {
                        std::cerr << "Roblox window not found." << std::endl;
                    }
                }
                else {
                    SetForegroundWindow(robloxHwnd);
                }
            }
        }
        else {
            if (isRightButtonDown) {
                keyState.ReleaseAll();
            }
            isRightButtonDown = false;
            cumulativeMouseX = 0; // Reset cumulative movement
            break;
        }

        if (raw->header.dwType == RIM_TYPEMOUSE && isRightButtonDown) {
            LONG deltaX = raw->data.mouse.lLastX;

            // Accumulate mouse movement
            cumulativeMouseX += deltaX;

            // Get the current time
            DWORD currentTime = GetCurrentTimeMs();

            // Check if enough time has passed since the last key switch
            if (currentTime - lastSwitchTime >= KEY_PRESS_DURATION) {
                if (cumulativeMouseX >= MOVEMENT_THRESHOLD) {
                    // Mouse moved right significantly
                    keyState.PressD();
                    keyState.ReleaseA();
                    cumulativeMouseX = 0;
                    lastSwitchTime = currentTime;
                }
                else if (cumulativeMouseX <= -MOVEMENT_THRESHOLD) {
                    // Mouse moved left significantly
                    keyState.PressA();
                    keyState.ReleaseD();
                    cumulativeMouseX = 0;
                    lastSwitchTime = currentTime;
                }
            }
        }
        break;
    }
    case WM_DESTROY:
        keyState.ReleaseAll();
        PostQuitMessage(0);
        return 0;
    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

int main() {
    BOOL isAdmin = FALSE;
    PSID administratorsGroup = NULL;
    SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
    if (AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
        DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0,
        &administratorsGroup)) {
        CheckTokenMembership(NULL, administratorsGroup, &isAdmin);
        FreeSid(administratorsGroup);
    }

    if (!isAdmin) {
        std::cerr << "Please run the application as an administrator." << std::endl;
        return 1;
    }

    const wchar_t CLASS_NAME[] = L"MouseCaptureWindow";

    WNDCLASS wc = { };
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = GetModuleHandle(NULL);
    wc.lpszClassName = CLASS_NAME;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);

    if (!RegisterClass(&wc)) {
        std::cerr << "Failed to register window class." << std::endl;
        return 1;
    }

    HWND hwnd = CreateWindowEx(
        0,
        CLASS_NAME,
        L"Strafe Helper",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 300, 100,
        NULL,
        NULL,
        GetModuleHandle(NULL),
        NULL
    );

    if (hwnd == NULL) {
        std::cerr << "Failed to create window." << std::endl;
        return 0;
    }

    RAWINPUTDEVICE rid;
    rid.usUsagePage = 0x01;
    rid.usUsage = 0x02;
    rid.dwFlags = RIDEV_INPUTSINK;
    rid.hwndTarget = hwnd;

    if (!RegisterRawInputDevices(&rid, 1, sizeof(rid))) {
        std::cerr << "Failed to register raw input device." << std::endl;
        return 1;
    }

    ShowWindow(hwnd, SW_SHOWMINIMIZED);

    std::cout << "Strafe Helper is running.\n"
        << "Hold down the RIGHT mouse button and move the mouse left or right to strafe.\n"
        << "Press ESC to exit.\n";

    std::atomic<bool> running(true);
    std::thread exitThread([&running]() {
        while (running.load()) {
            if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
                PostMessage(NULL, WM_CLOSE, 0, 0);
                running = false;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(50));
        }
        });

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    running = false;
    if (exitThread.joinable()) {
        exitThread.join();
    }

    return 0;
}