How to loop a set of commands in a keyboard emulator on on pro micro

Hi i'm supper new and have a question, so far i've built this entire command and ordered this arduino from the internet without any prior teaching on it but am stuck now. i tried searching before asking.
i am using an arduino pro micro to to emulate a keyboard and type a set of commands on my work computer to save me hours of work.

my goal is to plug in the usb push the button that makes connection when pushed. start this line of code. then to repeat over and over until the button is pushed again. what i have so far is

#include <Keyboard.h> // This is a "built-in" library no need to install

//---------------------------------------------------------
// Setup
//---------------------------------------------------------

void setup() {
pinMode(3,INPUT_PULLUP); // sets pin 3 to input & pulls it high w/ internal resistor
Serial.begin(9600); // begin serial comms for debugging

}

//---------------------------------------------------------
// Loop
//---------------------------------------------------------

void loop() {

Keyboard.begin(); //begin keyboard
if (digitalRead(3) == 0) // if buton 3 is pushed
{
Keyboard.write(0xD9); // down arrow
delay(200); //
Keyboard.write(0x82);
delay(200);
Keyboard.press('p');
delay(200);
Keyboard.releaseAll();
delay(200);
Keyboard.write(0x82);
delay(200);
Keyboard.press('i');
delay(200);
Keyboard.releaseAll();
delay(200);
Keyboard.write(0x82);
delay(200);
Keyboard.press('f');
delay(200);
Keyboard.releaseAll();
delay(200);
Keyboard.press(0xB3);
delay(200);
Keyboard.releaseAll();
delay(200);
Keyboard.press(0xB3);
delay(200);
Keyboard.releaseAll();
delay(200);
Keyboard.press(0xB3);
delay(200);
Keyboard.releaseAll();
delay(200);
Keyboard.press('y');
delay(200);
Keyboard.releaseAll();
delay(200);
Keyboard.press(0x82);
delay(200);
Keyboard.press('s');
delay(200);
Keyboard.releaseAll();
delay(200);
}
Keyboard.end(); //stops keybord
}

any help would be appreciated and any other pointers. the code works fine but i have to push the button again every time which wouldnt save anytime?

Welcome to the forums. Please read the sticky posts at the top of the forum about how to properly post your code using code tags. It helps others help you...

You have to remember that loop() runs again and again so you have to have a global variable that remembers what state you are in (waiting for button press to start or waiting for button press to stop). Also have a look at the example sketch File->Examples->02Digital->StateChangeDetection for how to detect the changes.

Also, I don't know much about the Keyboard library but it seems unlikely that you want to begin() and end() multiple times. begin() functions are usually called within setup()... But like I said, I don't know for this library.

The state change detection example shows how to detect that a switch HAS BECOME pressed or HAS BECOME released. The HAS BECOME part is crucial to your application, rather than the IS test that you are performing now.

Start the action when the switch HAS BECOME pressed. Stop when the switch HAS BECOME pressed again. The example counts the presses and uses a switch statement with the number of presses, but setting/clearing a boolean when there are only two cases is easier to follow.