| Reference | Downloads | Github

event.clearEvents() purpose?


I was advised to include event.clearEvents() into my code. However, I am not entirely sure what its purpose is. If I remove it, none of the data in my experiment appears to have changed. Can someone explain?

Also, is the code below an efficient way of capturing one’s reaction time, or is there a better way.

Any help is appreciated.



    starts = win.flip()

    response = event.waitKeys(keyList = ['space', 'return'], timeStamped = True)

    reaction_time = response[0][1] - starts
1 Like

You don’t need to use event.clearEvents() in this particular case, as the first thing the event.waitKeys() function actually does is call event.clearEvents() itself. i.e. it isn’t actually a problem for you to call it here, it’s just that it’s redundant.

What does event.clearEvents() do? Keypresses get stored in a buffer, because it is unlikely that you are checking the keyboard at the exact moment that a key is pressed. Hence the waitKeys() function uses clearEvents() so that it won’t be triggered by a key that was pressed and stored in the buffer prior to the waitKeys() function itself actually being called.

Where it is useful in your own code is if you use the event.getKeys() function instead of event.waitKeys(). You usually call event.getKeys() in a reasonably tight loop (say, on every screen refresh), so that it will capture a key that was pressed in the few milliseconds since the last check occurred. But what you don’t want it to do is have it immediately (i.e. on the first iteration) get triggered because there was a keypress waiting in the buffer that was actually placed there quite some time ago.

e.g. at the end of a fixation stimulus period, you might draw a target, call event.clearEvents() and then start looping through calls to getKeys(). This would ensure that you only respond to a key that was actually pressed after the onset of the target, and discard any that may have been pressed during the fixation period (which could give you negative reaction times).



Great answer.

I was just a bit worried as my tutor used event.clearEvents() in an example script - yet never actually used event.getKeys() anywhere, which got me a bit confused as to why he decided to use it.

When in doubt, you can always inspect the source code in your own copy of PsychoPy, or online at GitHub:

1 Like

Great answer, @Michael!

I just wanted to shortly add the purpose of event.getKeys() as I use it. I think you should always use event.waitKeys() unless a) you want to wait a fixed amount of time and not until button press (e.g., for an MRI experiment) or b) you are potentially interested in more than one response.

Here are two code examples for both cases:

a) Wait fixed amount of time irrespective of response

core.wait(3) #always wait for 3 sec regardless when response was made

b) Trigger several responses at once

    while True:
        ##core.wait(1) #optional wait if updating should not happen too often
        responses = event.getKeys()
        #responses 1-3 may trigger actions simultaneously
        if responseKey1 in responses: doThing1()
        if responseKey2 in responses: doThing2()
        if responseKey3 in responses: break

Why does event.getKeys() not implicitly call event.clearEvents()? Because sometimes you want to “remember” the old responses to build up upon them:

while True:
    responses = event.getKeys()
    if responseKey1 in responses: stimulus1.draw() #if responseKey1 was pressed, stimulus1 will remain until responseKey3 is pressed
    if responseKey2 in responses: stimulus2.draw() #same for responseKey2 and stimulus2 resp.
    if responseKey3 in responses: event.clearEvents() #stimulus1 and stimulus2 will disappear after the next iteration until the respective responseKeys are pressed again

I hope this further clarifies it for you.

Best regards,

1 Like