| Reference | Downloads | Github

Need some clarifications regarding when to clear the keyboard buffer

Hi community,

I use the new Keyboard function from Psychtoolbox, and I usually clear the buffer just after starting the clock to measure response time:

resp = keyboard.Keyboard() #initialize keyboard

#later in the experiment:

I just looked at the source code and noticed that the resp.getKeys() function (by default) clears anything that shows up in the buffer (after detecting it). This makes me wonder if I really need to call resp.clearEvents(eventType=‘keyboard’). Removing this call doesn’t seem to change anything. I am confused because the code generated by the builder also calls resp.clearEvents to clear the buffer when starting a clock. Could you please clarify?

I guess the difference is that .clearEvents() is intended for use when the Keyboard object was instantiated quite some time ago, and so it might have events in its queue that you want to ignore. For example, the subject might have pressed keys before the start of a trial, say. It would be a common thing to create a single keyboard object and use it across many trials, so clearing it at the start of each trial removes any keypresses that might have occurred after the primary response on the previous trial, for example.

When you call .getKeys(), generally you want by default to clear the queue, so that events don’t stay there for the next time you call .getKeys().

i.e. .clearEvents() is for clearing out the buffer of earlier events that you never wanted to collect in the first place, and .getKeys(clear = True) is so that you don’t process the same keypress more than once.

I guess there is an option for .getKeys(clear = False) for things like you want to end a trial after, say, five keypresses, and keeping them in the buffer means you don’t have to maintain a separate list, appending each one to it individually.

1 Like