You are struggling with event driven programming. You don’t wait; you call.
Look at this Assigning value from getKeyValue to a variable
Very little in your program should reside outside of a function. It all chains from events that are set into motion by clicking on something.