how to make an animation run continuously while you press a button on the keyboard

Hello everyone, I am new to the topic of development and I have a project in which I am starting and knowing the engine, maybe my question is, this time well, I do not know all the variables of the interface very well.

What I want to know is how to make an animation run continuously while you press a button on the keyboard, this is what I want to say because you are given a game, the animation repeats itself runs once more, and if you are a example, press a key to run, the animation to run only once, and keep moving forward without playing the animation continuously

or the animation for example, I want it to run continuously while in that state. What is the configuration issue? How does it work? What should I do? I thank you in advance for your help

Didn’t test it though, and typed it on note pad

bool flag;

void ButtonDown {
    if(not flag) {
        <Start animation>
        flag = true;
    }
}

void ButtonUp () {
    <Stop animation>
    flag = false;
}