Keyboard press response time from trial onset with coding component

OS : Mac Mojave 10.14.6
PsychoPy version (e.g. 1.84.x): psychopy 3 v2021.2.3

What are you trying to achieve?:

I am running a time reproduction experiment where Ps hold down the space bar to reproduce the time that they thought a stimulus appeared on screen. Note that I have built this experiment in builder, but with code components.

The total time that Ps held the space bar down has been recorded in the data file using a code component, which is called ‘Hold Times’ in my data file, however, because I have not used a keyboard component in builder the psychopy I do not have the option in to select the box for recording the response time from the beginning of the trial to pressing the space bar.

Please see the code below for the response time from trial onset to space bar press (key.rt) and total time spent holding down the space bar (duration). The duration is being recorded successfully, but not the response time from trial onset to space bar press, so any help would be much appreciated.

Begin Routine:
duration=
key.rt=
kb = keyboard.Keyboard()

Each Frame
keys2 = kb.getKeys()
for key in keys2:
if key.duration:
duration.append(key.duration)
keys2 = kb.getKeys()
for key in keys2:
if key.rt:
rt.append(key.rt)

End Routine:
Practice_Trials_Loop.addData(“Hold Times”, duration)
Practice_Trials_Loop.addData(“Response Time”, rt)

I have looked at a number of forum posts but no luck with getting this working:

Thanks in advance!

I think it’s a couple things, both of them in your “Begin routine” code.

First, in your “Begin routine” code, you define a list called ‘key.rt’. I think you just want to call this list ‘rt’, based on the fact that you’re adding to a list that is just called ‘rt’ in your “Each frame” code, and it’s this ‘rt’ list which is supposed to be stored in the data.

Second, to get RT you need to give your keyboard a clock when you initialize it, so it has a zero-time to compare against.

So try this in your “Begin routine” code:

duration = []
rt = []
trialClock = clock.Clock()  # if this doesn't work for some reason use core.Clock() instead
kb = keyboard.Keyboard(clock=trialClock)

I think you should be able to leave everything else the same.

Hi Jonathan,

Thank you for your reply! I tried both clock.Clock() and core.Clock() on the 3rd line of the Code in Begin Routine and unfortunately neither worked as I get a in the data file for the variable Response Time.

I have attached my experiment below. Let me know if you have any other potential solutions as I am at a loss :sweat_smile:

Entropy Experiment FIlePractice.psyexp (42.0 KB)

Ah, I think I see it: When you call getKeys() it clears the buffer, so you’ve gotten both the rt and the duration the first time you call it in your ‘Each Frame’ code, but then you call getKeys() a second time everything just gets overwritten with a blank, so there’s no keys and no RT when you go to add the RTs.

I recommend the following for your ‘Each Frame’ code:

keys2 = kb.getKeys()
for key in keys2:
    if key.duration:
        duration.append(key.duration)
    if key.rt:
        rt.append(key.rt)

I think that will do it, though I don’t have time to test it myself I’m afraid.

Thank you so much Jonathan this has worked :slight_smile:

Note to those seeing this post that the hold time and response time works when run in builder, but not on pavlovia!

See my post here Code component for additional data columns not working in Pavlovia (but does in Builder) - #11 by wakecarter