Recording keyboard response time relative to target stimulus onset time in a target detection task

Hello.

I’m creating a target detection task where I present a series of sound stimuli one at a time and the subject needs to respond to the “target items” by pressing the space bar. The task loops through a routine that has a stimulus and a keyboard component. I’m trying to record the keyboard response time relative to the onset time of the target item.

So far, I have set a timer at the beginning of the experiment and inserted the following code at “Begin Routine”.

if Sound == Target:
    targetOnset = Traintimer.getTime()

if TrainResp.keys in ['', [], None]:
    TrainResp.keys = None
if TrainResp.keys != None:
    responseOnset = Traintimer.getTime()
    reactiontime = responseOnset - targetOnset

It is recording the onset time for both the target stimulus and keyboard press, but the timing doesn’t seem to be accurate. I always get a RT of 300ms (which is the length of the sound stimulus) when I know I’m pressing the keyboard at the same time as the target onset (and should be getting a shorter RT).
I have also tried inserting the Traintimer.getTime() part in different places in the coder view but I’m still getting the same problem.

Is using the timer the right way to do this, and if so, where should I insert it?

Thank you!!