psychopy.org | Reference | Downloads | Github

Incorrect time stamps with event.getKeys()

I have this small snippet of code:

timeSinceStart = core.MonotonicClock()

event.clearEvents('keyboard')
core.wait(TIMING_INFO['response_time'])
displayFixation()
kbInputs = event.getKeys(timeStamped = timeSinceStart)

While a response screen and fixation are displayed, the system is recording all keys pressed. These key presses should all be timestamped with a MonotonicClock object however their outputted timestamps are incorrect. All key presses were recorded successfully, however it appears that calling core.wait() also prevents getKeys() from getting the correct timestamp. The output looks something like this:
(‘a’,2.997), ('b, ‘2.998’), (‘c’, 17.899), (‘d’, 17.999)
It’s important to note that two core.wait() functions are called during this key logging, one for 3 seconds and the other after it for 15 seconds. To me, it appears regardless of when the key is pressed, it’s timestamp is associated at the end of a core.wait() almost as if the two can’t be done at the same time. I was wondering if anyone had a good work around for this, or better method for collecting keyboard input in this manner?

Yes. Wait forces everything to stop. You can use the newer Keyboard class (uses psychHID from psychtoolbox) or ioHub, both of which continue polling in a separate thread. If you use Builder to create your studies this sort of thing will be handled for you automatically :wink:

2 Likes

Thanks for the suggestion!

For those wondering my solution, while definitely not the most efficient, I use a while loop which has a short delay (roughly 10ms) that will continue to loop until a monotonic clock element has had a certain amount of time elapse. Inside this loop I run the event.getKeys() time stamped with the monotonic clock, this seemed to be accurate enough for our use case.

startTime = core.MonotonicClock()
while startTime.getTime() < waitTime:
   event.getKeys(timeStamped = startTime)
#...
   core.wait(0.01)