Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Unwanted jitter in keyboard response

Hi everyone, I'm having an issue with timing of my response collection, your help would be much appreciated!

I've implemented a task using an inline script where keyboard responses are collected. Participants make multiple key presses, which should be collected until a maximum reaction time is reached. However, I can't seem to find a way to implement this without getting jittered max rt's. Below is a simplified version of what I'm trying to do: 

max_rt = 3000 # maximum reaction time

my_keyboard = keyboard(keylist=["1", "2"])

t0 = clock.time() # start tracking duration of trial

resp=[] # for storing keyboard responses each trial
while clock.time() - t0 < max_rt: 	# collect responses until time's up
	# get keyboard responses
	key = my_keyboard.get_key(timeout=max_rt)

print("rt given: " + str(clock.time() - t0))

This is implemented in a loop sequence, in which it is preceded by a fixation dot.

Using this code, when there is no key press, the correct max rt is given. However, after some key presses, the while loop continues well past the max rt (I've seen it go over 5600 when set to 3000). It happens even when displaying a blank canvas, and changing backends doesn't make a difference.

I've tried implementing it in other ways to no avail, for example changing the conditions of the while loop:

while True: 	
	if clock.time() - t0 >= max_rt:

Does anyone know why this is happening, and is there another way to implement this in an inline script (it's important in my case to do so)?


  • Hi @phidget ,

    Can you try to see if parentheses around your arithmetic have an effect? So change

    while clock.time() - t0 < max_rt: 


    while (clock.time() - t0) < max_rt: 

    my hunch is that this is a precedence issue, as the < and > operators might have precedence over the - , which makes you effectively first evaluate `t0 < max_rt`

  • Hi @Daniel,

    Thanks for your response. Unfortunately adding parentheses does not solve the issue.

  • Hi, I think the reason is that your while loop has a timeout of 3000 ms, but your response collection as well. Therefore, it could happen that the response collection finishes running, but the while loop has not yet finished, and the next response collection is initiated, so that you would have to wait for another 3000 ms. In total, a delay until up to 6000 would be possible. Why you get numbers like 5600 ms, I don't quite understand though.

    In any case, if you set the timeout of the response collection to 0 ms, you would get the behaviour that you want to have I think. Can you try that?


  • Thanks eduard, you were right, the reason for the extra time given was due to having a timeout in the while loop condition as well as in the argument to the get_key function.

    Changing the keyboard timeout to 0 makes the loop never end until a key is pressed regardless of the while loop condition. But I was able to fix the issue by changing the timeout of the response collection to the following:

    key = my_keyboard.get_key(timeout=max_rt-(clock.time()-t0)) 

    Thanks for your help!

Sign In or Register to comment.