# event.clearEvents() purpose?

**URL:** <https://discourse.psychopy.org/t/event-clearevents-purpose/2272>\
**Category:** Coding\
**Created:** [April 5, 2017, 10:49pm UTC](https://discourse.psychopy.org/t/event-clearevents-purpose/2272 "2017-04-05T22:49:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Polar\_Py](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@Polar\_Py](https://discourse.psychopy.org/u/Polar_Py)\
**Post date:** [April 5, 2017, 10:49pm UTC](https://discourse.psychopy.org/t/event-clearevents-purpose/2272/1 "2017-04-05T22:49:23Z")

</div>

Hello.

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.

```auto
    stim.draw()

    event.clearEvents()

    starts = win.flip()

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

    reaction_time = response[0][1] - starts

```

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [April 5, 2017, 11:27pm UTC](https://discourse.psychopy.org/t/event-clearevents-purpose/2272/2 "2017-04-05T23:27:08Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Polar\_Py](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@Polar\_Py](https://discourse.psychopy.org/u/Polar_Py)\
**Post date:** [April 5, 2017, 11:47pm UTC](https://discourse.psychopy.org/t/event-clearevents-purpose/2272/3 "2017-04-05T23:47:13Z")

</div>

Thanks!

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.

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [April 5, 2017, 11:52pm UTC](https://discourse.psychopy.org/t/event-clearevents-purpose/2272/4 "2017-04-05T23:52:38Z")

</div>

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

> <https://github.com/psychopy/versions/blob/master/psychopy/event.py#L313>

---

<div class="post-metadata">

**Author:** ![mario.reutter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mario.reutter/32/5750_2.png) [@mario.reutter](https://discourse.psychopy.org/u/mario.reutter)\
**Post date:** [April 12, 2017, 9:02am UTC](https://discourse.psychopy.org/t/event-clearevents-purpose/2272/5 "2017-04-12T09:02:43Z")

</div>

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

```auto
stimuli.draw()
window.flip()
event.clearEvents()
core.wait(3) #always wait for 3 sec regardless when response was made
event.getKeys()

```

b) Trigger several responses at once

```auto
stimuli.draw()
window.flip()
    
    while True:
        event.clearEvents()
        ##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:

```auto
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
    window.flip()

```

I hope this further clarifies it for you.

Best regards,  
Mario
