Skipping an image if subjects press a button

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2020.1.2

Hi, i am working on a Pavlovian conditining experiment. Its and avoidance paradigm where a CS (an image of a colored lamp is shown for 12 seconds) is followed by an aversive image from the International Aversive Picture System (IAPS) for 4 seconds.
Subjects should be able to avoid the IAPS image by pressing a button (space bar) in a 2 seconds window during the CS presentation. A red button picture is shown during this time window.
I’m having trouble getting the trial to skip the IAPS image and start the next trial when the CS presentation ends, if the subjects pressed the space bar.
Right now i have 2 different CS images with their respective IAPS image and a third CS that goes with a neutral image, as a control. I use an excel file in a loop to change the images on each trial.
In the builder, i set up a trial with the onsets of a background (NCUE), the CS (esCond), the button (boton), the IAPS or neutral image (US), a keyboard component to log responses and a Code Component where i’m trying to program the avoidance.
In the beginning tab i wrote:

key = ()

Then, in each frame

if estCond.image == ‘4.bmp’ :
key = key_resp.getKeys([‘space’])
if ‘space’ in key:
key = ()
continueRoutine = False

I figured that “estCond.image” contains the name of the CS image for a given trial. “4.bmp” is the avoidance CS.
This results in that any space key press will end the trial and start the next one.
I tried using a core.clock() to time when i wait for key presses, but i learned that the key_resp.getKeys([‘space’]) line doesn’t work when the key_resp component is active.
how could i skip the IAPS image presentation on a given trial when only a certain image is the CS and the subject pressed a button?

I would use the variable you use to sent estCond.image rather than trying to query it, so…

if US == '4.bmp':
     key=event.getKeys()
     if 'space' in key:
          continueRoutine=False

Some extra code might be needed to limit the range of time that this key can be pressed.

key doesn’t need to be reset since it automatically clears after every key press. If there are unrecorded keys from earlier then you might need to clear the buffer with event.clearEvents() first.