Toggle key to start and pause program

I’ve got a program, in java, that is just a simple autoclicker. What I’m looking to do is make it so that with the press of a key (say, F9) the infinite loop in the program runs without interruption – and when another key (say, F10) is pressed, the program pauses, such that once I press F9 again the program resumes. Here’s the code that I’ve got:

package SimpleCode;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.MouseEvent;

public class AutoClicker {

 public static void MainFunction(){
  while(true){
   leftClick();
   delay(6.5);
  }
 }

 protected static void delay(double seconds){
  createMacro();
  macro.delay((int(seconds * 1000.0));
 }

 protected static void leftClick(){
  createMacro();
  macro.mousePress(MouseEvent.BUTTON1_MASK);
  macro.mouseRelease(MouseEvent.BUTTON1_MASK);
 }

 private static Robot macro = null;

 private static void createMacro(){
  try {
   macro = new Robot();
  } catch (AWTException e) {
   e.printStackTrace();
  }
 }

 public static void main(String args[]){
  MainFunction();
 }

}

I suppose to interrupt the loop, I must find a way to make the “while(true)” part false once F10 is pressed, but that’s all that I can reason. I’m not sure how pressing F9 would start the program.

Note: credits to SimpleCode’s youtube video on the subject for the above framework.