Mouse Does Not Move Automatically When Pressing Left Button

I'm coding a C++ program that makes a certain movement with the mouse by itself when I press the left mouse button, it has F1, F2, F3 keys and each key has a different movement, and then I press the left mouse button to execute that movement , but what is happening is that I have to hold down the F1 key and the left mouse button at the same time for it to work, I wanted to press the F1 key just once and then just hold down the left mouse button !

#include <iostream>
#include <string>
#include <Windows.h>
#include "patterns.h"

#define getLen(x) (sizeof(x)/sizeof(x[0]))

using namespace std;

typedef struct $ {
    string  name;
    long    **pattner;
    int     len;
    int     rpm;
}weapon_t;

    weapon_t *LoadWeapon(string name, long recoil[][2], int len, int rpm);
    float GetK(float dpi, float sensi);
    DWORD GetTime(float rpm);
    void ExecControl(weapon_t gun);

    float K;

int main()
{

    K = GetK(800, 0.95);

    weapon_t *ak47 = LoadWeapon("AK-47", ak47_pattern, getLen(ak47_pattern), 600);
    weapon_t *m4a4 = LoadWeapon("M4A4", m4a4_pattern, getLen(m4a4_pattern), 666);
    weapon_t *ump45 = LoadWeapon("UMP-45", ump45_pattern, getLen(ump45_pattern), 666);
    weapon_t* m4a1s = LoadWeapon("M4A1S", m4a1s_pattern, getLen(m4a1s_pattern), 600);
    weapon_t* famas = LoadWeapon("FAMAS", famas_pattern, getLen(famas_pattern), 600);

    while (true) {
        if (GetAsyncKeyState(VK_F1)) {
            ExecControl(*ak47);
        }
        if (GetAsyncKeyState(VK_F2)) {
            ExecControl(*m4a4);
        }
        if (GetAsyncKeyState(VK_F3)) {
            ExecControl(*m4a1s);
        }
        if (GetAsyncKeyState(VK_F4)) {
            ExecControl(*ump45);
        }
        if (GetAsyncKeyState(VK_F5)) {
            ExecControl(*famas);
        }
        Sleep(150);
    }

    return 0;
}

void ExecControl(weapon_t gun) {
    system("cls");

    cout << "Weapon:\t" << gun.name << "\nShots:\t" << gun.len << "\nVelocity:\t" << gun.rpm << "\n\n\n";

    DWORD delay = GetTime(gun.rpm);

    int index = 0;

    while (GetAsyncKeyState(VK_LBUTTON) && index != gun.len) {
        mouse_event(MOUSEEVENTF_MOVE, long(gun.pattner[index][0] * K), long(gun.pattner[index][1] * K), 0, 0);
        index++;
        Sleep(delay);
    }

    index = 0;
}

weapon_t *LoadWeapon(string name, long recoil[][2], int len, int rpm) {
    
    int i;

    weapon_t *gun = new weapon_t();

    gun->name = name;
    gun->len = len;
    gun->rpm = rpm;

    gun->pattner = new long*[len];
    for (i = 0; i < len; i++) {
        gun->pattner[i] = new long[2];
    }
    
    for (i = 0; i < len; i++) {
        gun->pattner[i][0] = recoil[i][0];
        gun->pattner[i][1] = recoil[i][1];
    }

    return gun;
}

float GetK(float dpi, float sensi) {
    return (1140 / (dpi*sensi));
}

DWORD GetTime(float rpm) {
    return DWORD((60 / rpm) * 1000);
}


The problem is that your code is looking at the mouse left button only while any F1..F5 key is currently held down, and then you are not looking at the keyboard again until the left button is released or the current gun is exhausted.

You need a different approach, such as a state machine. Keep track of the current gun that is equipped at any given time. On each iteration of the main loop, check for state changes first (ie, check if any F1..F5 key is held down, and if so then equip the associated gun), and then act on the current state (ie, regardless of the keyboard state, if the left button is down, and a gun is equipped, then advance the gun to its next position). Let the loop handle the repetitive work for you. This will allow you to check the keyboard for gun changes while the left button is held down.

Try something more like this:

#include <iostream>
#include <string>
#include <Windows.h>
#include "patterns.h"

#define getLen(x) (sizeof(x)/sizeof(x[0]))

using namespace std;

typedef struct $ {
    string  name;
    long    **pattner;
    int     len;
    int     rpm;
}weapon_t;

weapon_t* LoadWeapon(string name, long recoil[][2], int len, int rpm);
float GetK(float dpi, float sensi);
DWORD GetTime(float rpm);
float K = GetK(800, 0.95);

int main()
{

    weapon_t *ak47 = LoadWeapon("AK-47", ak47_pattern, getLen(ak47_pattern), 600);
    weapon_t *m4a4 = LoadWeapon("M4A4", m4a4_pattern, getLen(m4a4_pattern), 666);
    weapon_t *ump45 = LoadWeapon("UMP-45", ump45_pattern, getLen(ump45_pattern), 666);
    weapon_t *m4a1s = LoadWeapon("M4A1S", m4a1s_pattern, getLen(m4a1s_pattern), 600);
    weapon_t *famas = LoadWeapon("FAMAS", famas_pattern, getLen(famas_pattern), 600);

    weapon_t* guns[] = {ak47, m4a4, m4a1s, ump45, famas};
    int gun_keys[] = {VK_F1, VK_F2, VK_F3, VK_F4, VK_F5};
    weapon_t *gun = nullptr;
    int gun_index = -1;
    DWORD gun_delay = 150;
    bool is_mouse_down = false;

    while (true) {

        for(int i = 0; i < 5; ++i) {
            if (GetAsyncKeyState(gun_key[i]) && (gun != guns[i])) {
                gun = guns[i];

                system("cls");

                cout << "Weapon:\t" << gun->name << "\nShots:\t" << gun->len << "\nVelocity:\t" << gun->rpm << "\n\n\n";

                gun_delay = GetTime(gun->rpm);
                gun_index = 0;

                break;
            }
        }

        if (GetAsyncKeyState(VK_LBUTTON) < 0) {
            if (!is_mouse_down) {
                is_mouse_down = true;
                if (gun != nullptr)
                    gun_index = 0;
            }
            if (gun != nullptr  && gun_index != gun->len) {
                mouse_event(MOUSEEVENTF_MOVE, long(gun->pattner[gun_index][0] * K), long(gun->pattner[gun_index][1] * K), 0, 0);
                ++gun_index;
                Sleep(gun_delay);
                continue;
            }
        }
        else    
            is_mouse_down = false;

        Sleep(150);
    }

    return 0;
}

weapon_t* LoadWeapon(string name, long recoil[][2], int len, int rpm) {

    weapon_t *gun = new weapon_t();

    gun->name = name;
    gun->len = len;
    gun->rpm = rpm;

    gun->pattner = new long*[len];
    for (int i = 0; i < len; i++) {
        gun->pattner[i] = new long[2];
        gun->pattner[i][0] = recoil[i][0];
        gun->pattner[i][1] = recoil[i][1];
    }

    return gun;
}

float GetK(float dpi, float sensi) {
    return (1140 / (dpi*sensi));
}

DWORD GetTime(float rpm) {
    return DWORD((60 / rpm) * 1000);
}