Hi @ajus – thanks so much for this snippet of code.
I should have provided a bit more context in my initial post. On each trial, a fixation cross is presented and the participant presses and holds a button to advance the trial. An image is then presented and the participant lets go of the button if the image is consistent with task instruction (kind of like a go/no-go task).
So I would need the duration of time between the onset of the image and the de-pressing of the button.
Do you think the code provided would still work in this instance?
If the image appears immediately when the space bar is pressed, the duration of the key press should be pretty much identical to the time from the image onset to the “de-press” of the space bar. So, yes if the code works at all, it should do what you want
Maybe try this code. In contrast to my previous suggestion, it clears the keyboard at the beginning of the routine, just in case you use “space” elsewhere before the routine. Also, it ends the routine on the “de-press”, which is probably also what you want to do, right?
# Begin experiment
from psychopy.hardware import keyboard
from psychopy import core
kb = keyboard.Keyboard()
# Begin routine
kb.getKeys(waitRelease = False, clear = True)
# Every frame
keys = kb.getKeys(['space'], waitRelease=True)
if "space" in keys:
continueRoutine = False
# End routine
key = keys[0]
thisExp.addData("RT", key.duration)
Thanks again for your code. I’ve been trying it out and I’ve found that RT is accurately recorded only if I press the space bar. It would appear that PsychoPy is not registering that the button is first de-pressed before it is pressed again.
It also appears that trial level information may be overwritten on every trial. The CSV output only has information from one trial.
for me, the code works fine, see depress1 below. Is the space bar pressed and released in two different routines? If so, make sure to use the code in “Begin routine” in the first routine and the other code in the second one, see depress2. depress1.psyexp (12.1 KB) depress2.psyexp (14.8 KB)