Add background flash with Cedrus box

(macOS Sequoia 15.1.1)
(PsychoPy Standalone v2024.1.4, Builder view)

Hi, thanks for the help. I have a routine where participants watch a movie and press a Cedrus button box multiple times throughout (the button press does not end the routine). I want to add a background flash behind the movie as feedback for each button press.

I tested this with a keyboard space button using the custom code component below, which worked.

Begin routine:

win.color = 'black'  # Initial background color
flash = False       # Flag to control flashing
flash_timer = 0     # Timer to track how long the flash lasts
flash_duration = 0.4  # Duration of flash in seconds (adjust as needed)

Each frame:

# Detect space bar press
theseKeys = event.getKeys(keyList=['space'])

# Initialize flash when space is pressed
if 'space' in theseKeys:
    win.color = 'Gainsboro'  # Start with white flash
    flash_stage = 1  # Stage 1: white
    flash_timer = t  # Start timing the flash

# Proceed with flash stages if flash has been initiated
if 'flash_stage' in locals():  
    if flash_stage == 1 and t - flash_timer >= 0.1:  # After 0.1s switch from white to grey
        win.color = 'grey'
        flash_stage = 2  # Move to stage 2
        flash_timer = t  # Reset timer for next stage

    elif flash_stage == 2 and t - flash_timer >= 0.1:  # After 0.1s switch from grey to black
        win.color = 'black'
        del flash_stage  # End flash sequence
        del flash_timer  # Clean up the timer

I couldn’t get this to work with the Cedrus button box. I tried adding this into Begin routine: buttonBox = cedrusBox_0, and replacing the first line of Each frame with: theseKeys = buttonBox.getKeys()

I get this error:

    return getattr(self._impl, attrname)
AttributeError: 'NoneType' object has no attribute 'getKeys'
################# Experiment ended with exit code 1 [pid:7178] #################

Any ideas about how to get this working?
Thank you!

How do you normally get responses from the Cedrus buttonBox?

It could be that you just need to check buttonBox exists first with if buttonBox:.