Code component in builder to record total time that the spacebar was held down for and record this in data file

Hi all

I am running an experiment on time perception & entropy. I need two things:

  1. Ps have to estimate time duration of a stimulus by pressing and holding the space bar, releasing the space bar once the estimated time duration ends. I would like to know the duration of Ps holding down the space bar. How can this be implemented using python code?
  2. I also would like this recorded in the csv data file so that it can be extracted for analysis. How can this be implemented using python code?

I have read a few different forums from a couple of years ago but none have provided me with an answer - it seemed that quite a few people were having the same issue so it would be great to here if there has been a recent solution.

I have built this experiment in builder and am aware that I need to add to snippet of code to my experiment for the time reproduction component in the trial loop. I am new to coding and prefer python over JS.

I currently have a blank text component (as a blank grey screen is their cue to start to reproduce the time) which I have on screen for 6 seconds. I also a keyboard component for this event defining the spacebar as the key press with the force end of routine box not ticked. This is so Ps have up to 6 seconds to respond until the trial moves on.

Any help would be much appreciated.

Solved here: Measuring key lift time relative to stimulus onset - #4 by milsandhills