One animation for character controller

I’ve got a cube gameobject as my character model, I only have one control which is forward, this moves the cube +1 in a given direction. What I’m trying to hopelessly do is create a simple animation in Unity then play it when the control is pressed. I’ve made the animation however the cube is constantly playing the animation in a loop from the moment I press play. How do I limit it to only play when the control is pressed. Also how would I time it so that the time it takes the cube to move to the new location times in with the animation? Currently the cube just teleports +1 infront of its position which means the animation would show as playing when its in its new position however I want the animation to transition between the move. Any pointers would be helpful. All the tutorials/links I’ve found so far haven’t helped or have just confused the hell out of me.

in pseudo code

if(fwd is pressed){
   play animation of running
}
else{
   stop animation of running and/or play idle animation
}