PsychoPy- event.getKeys() is moving to next stimulus without running code needed for next stimulus decision

If this template helps then use it. If not then just delete and start from scratch.

OS MacOS
PsychoPy version (e.g. 1.84.x): v2020.2.10
**What are you trying to achieve?: Keyboard input needs code run to decide what stimulus should be shown next.

**What did you try to make it work?: Tried wrapping in a while loop

**What specifically went wrong when you tried that?:
For some reason when event.getKeys() is run in the 5th line below it immediately goes to the next stimulus shown on the screen without using all the code below to help decide which stimulus is appropriate to be shown next. But, what’s happening is the code is being run, but AFTER it presents the next stimulus. So basically it’s an off-by-one error because every “next stimulus” is using data from the previous stimulus selection. I’ve tried to wrap all of this in a while loop but it still doesn’t work. So, lets say someone presses ‘2’ for a happy face to show up. Then next trial they press ‘7’ for a sad face. When they press that 7 it will display a happy face because that was the previous input key (2) selection. Then, whatever they select next will always have a sad face because 7 was pressed previously. All of the code is not running before moving on to the next presented stimulus. Thank you for any help.

# random numbers for choosing a picture and Happy/Sad
rand = random.randint(1,140)

# acquire response key for bias
key = str(event.getKeys())

# check is bias direction equals key selection
if bias == key: 
    print ("choosing H or S")
    # get happy/sad for bias
    biasPercent = random.random() # check for 90/10%
    if biasPercent <= 0.90:
        biasEmotion = "S"
    if biasPercent > 0.90:
        biasEmotion = "H"
    print ("..................biasEmotion = " + biasEmotion)

if bias != key: 
    print ("choosing H or S")
    
    # get happy/sad for bias
    biasPercent = random.random() # check for 90/10%
    if biasPercent <= 0.90:
        biasEmotion = "H"
        happyFaceCount = happyFaceCount + 1
    if biasPercent > 0.90:
        biasEmotion = "S"
        sadFaceCount = sadFaceCount + 1
    print ("..................biasEmotion = " + biasEmotion)
    
comparison = False
iteration = 1
# chooses a random face from excel column A
while (imgName in listOfFacesHappy) or (imgName in listOfFacesSad) or comparison:
    print (str(iteration) + "  iteration")
    iteration = iteration + 1
    rand = random.randint(1,140)
    face = insheet.row_values(rand)
    imgName = face[0] # Column A (index 0) from excel
    sexIndex = imgName[1] # second char in name of file
    emotionIndex = imgName[4] # fifth char in name of file
    if (biasEmotion != emotionIndex):
        print ("emotions not equal")
        comparison = True
    else:
        print ("..............found emotion")
        comparison = False
      
if emotionIndex == "H":
    listOfFacesHappy.append(imgName)
    happyFaceCount = happyFaceCount + 1
if emotionIndex == "S":
    listOfFacesSad.append(imgName)
    sadFaceCount = sadFaceCount + 1
#print ("face list = " + str(listOfFaces))

# names currently hardcoded and not taken from excel or text file yet
if sexIndex == "F":
    nameR = random.choice(fnames)
    nameL = random.choice(fnames)
    while nameL == nameR:   #make sure two names are different
        nameR = random.choice(fnames)
if sexIndex == "M":
    nameR = random.choice(mnames)
    nameL = random.choice(mnames)
    while nameL == nameR:   #make sure two names are different
        nameR = random.choice(mnames)

Is this in a Builder code component or should this question be in the Coder category?