Selecting image from carousel

Hi,

I’m incredibly new to PsychoPy and any type of coding and was hoping someone would be able to help.
I have an experiment in which I have multiple images of a food incrementally increasing in size through which participants can use the left and right arrow keys to move through. I would like them to be able to press the spacebar to select one of these images of which they would consider their normal portion size. Currently I have code that allows me to present the images and have participants move from left to right, but I’m not sure how to add the spacebar press in so that it records which image in the list it was pressed on, and then also move on to the next routine.
My code looks something like this

Begin Routine

listOfImages = ["food_100g","food_110g","food_120g","food_130g"]
# Create a new keyboard
key_resp = keyboard.Keyboard()
# List index
idx = 0
# Set the value of the text or image
nextImage = listOfImages[idx]

Each Frame

# Get keypress
theseKeys = key_resp.getKeys(keyList=['left', 'right'])

# Go backwards or forwards
if theseKeys:
    if theseKeys[0].name == 'left' and idx > 0:
        idx -= 1
    elif theseKeys[0].name == 'right' and idx < (len(listOfImages) - 1):
        idx += 1

# Set the variable feeding the text component
nextImage = listOfImages[idx] 

any help would be much appreciated!

# Get keypress
theseKeys = key_resp.getKeys(keyList=['left', 'right', 'space'])

# Go backwards or forwards
if theseKeys:
    if theseKeys[0].name == 'space':
        # record the choice in the data:
        thisExp.addData('chosen_index', idx)
        thisExp.addData('chosen_image', listOfImages[idx]) # assuming this is a string?
        continueRoutine = False # and finish the trial
    elif theseKeys[0].name == 'left' and idx > 0:
        idx -= 1
    elif theseKeys[0].name == 'right' and idx < (len(listOfImages) - 1):
        idx += 1

# Set the variable feeding the text component
nextImage = listOfImages[idx]