How to end a loop until it is changed

Hey all,

So I'm new to arduino, started last night. I pieced together this code that has the Leonardo Pro Micro controller hit ctrl+f11 whenever pin9 hits ground, and ctrl+f12 when pin8 hits ground. Then it delays 1000 to stop the code from repeating a bunch of times while the momentary switch controlling is is depressed.

This code, along with an autohotkey script makes it so whenever i hit one of these switches, the audio source changes to either my TV or headphones.

My goal is to change this code to allow myself to use any type of switch, and not just momentary. I want to add code so whenever pin 9 or 8 are triggered, the loop stops until there is a change, so if a switch is flipped to pin9, the controller doesn't keep telling autohotkey to keep running the batch file to set the audio source.

Here's the code:

#include <Keyboard.h>

int pin9 = 9; // Headphone Button
int pin8 = 8; // TV Button
char ctrlKey = KEY_LEFT_CTRL;
char F11 = KEY_F11;
char F12 = KEY_F12;

void setup()
{
pinMode(pin9, INPUT); // Set the button as an input
digitalWrite(pin9, HIGH); // Pull the button high
pinMode(pin8, INPUT); // Set the button as an input
digitalWrite(pin8, HIGH); // Pull the button high
}

void loop()
{
if (digitalRead(pin9) == 0) //
{
Keyboard.begin();
Keyboard.press(ctrlKey); // press Left Ctrl
Keyboard.press(F11); // press F11
Keyboard.releaseAll();
delay(1000); //
}

if (digitalRead(pin8) == 0) //
{
Keyboard.begin();
Keyboard.press(ctrlKey); // press Left Ctrl
Keyboard.press(F12); // press F12
Keyboard.releaseAll();
delay(1000); //
}
}

TLDR: Need code to stop a loop until there is a change in that loop.

Thanks in advance, and sorry if this is posted in an incorrect area, or a really dumb question. As I said, I started this stuff last night and trying to read up on the "while" command really confused me.

Check the state change example in the IDE