Measuring key press time accurately

Hi all,

I am coding an fMRI experiment and need to measure trigger time (emulated as key press) as accurately as possible. I have compared recoding the time from psychtoolbox GetSecs() and clock.GetTime() with the .rt values from kb.waitKeys. There is a discrepancy of around 60-70 ms between both methods and I am trying to figure out which value is the more precise one. Would anyone now?

Thanks and best,
Jasmin

trigger_kb = keyboard.Keyboard()
ptb_start = ptb.GetSecs()
trigger_kb.clock.reset()

keys = trigger_kb.waitKeys(keyList=[‘t’], waitRelease=False)
key_ptb_time = ptb.GetSecs()
key_rt_clock = trigger_kb.clock.getTime()
key_rt_kb = keys[0].rt

diff = key_ptb_time - ptb_start - key_rt_kb
print(f"key.rt: {key_rt_kb:.6f} s")
print(f"clock.getTime: {key_rt_clock:.6f} s")
print(f"PTB timestamp: {key_ptb_time - ptb_start:.6f} s")
print(f"Difference: {diff:.6f} s")

Regardless of the method you use, the biggest factor of latency is the hardware you use (what type of keyboard/button box) and the USB connection type (USB 1, 2, 3.0, 3.1, 3.2).

The differences in time you are observing here could be because you are calling things at different times. But that would only be very small differences. From previous experience, psychtoolbox and psychopy’s internal clocks work differently than each other, so it’s tough to compare them when they are functionally two different clocks.

Issac

Just in case anyone is interested: using keyboard.Keyboard(backend='ptb')explicitly reduced the difference to a sub ms difference, whereas not setting the backend explicitly produced 50-70 ms differences consistently. I assume that this would be the most reliably time stamp.