# Abort Trial If Mouse Stops Moving

**URL:** <https://discourse.psychopy.org/t/abort-trial-if-mouse-stops-moving/18691>\
**Category:** Coding\
**Created:** [December 4, 2020, 7:01pm UTC](https://discourse.psychopy.org/t/abort-trial-if-mouse-stops-moving/18691 "2020-12-04T19:01:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kellyheom](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kellyheom](https://discourse.psychopy.org/u/kellyheom)\
**Post date:** [December 4, 2020, 7:01pm UTC](https://discourse.psychopy.org/t/abort-trial-if-mouse-stops-moving/18691/1 "2020-12-04T19:01:52Z")

</div>

I honestly have no idea how to start writing this. I understand there is the  
`mouseMoved`  
but this only looks at the distance. I want to abort the trial if the mouse stops moving at all. Thoughts?

---

<div class="post-metadata">

**Author:** ![tandy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tandy/32/9246_2.png) [@tandy](https://discourse.psychopy.org/u/tandy)\
**Post date:** [December 5, 2020, 8:15am UTC](https://discourse.psychopy.org/t/abort-trial-if-mouse-stops-moving/18691/2 "2020-12-05T08:15:39Z")

</div>

Hi, spitballing here!

Maybe you could try having a counter that start at a value “x” and gains an unit of value at a fixed time (e.g. 1 every 100 msec). If the counter reaches a certain value the trials stops.

Then, moving the mouse removes some value from the counter, up to a minimum of “x”.

tandy

---

<div class="post-metadata">

**Author:** ![kellyheom](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kellyheom](https://discourse.psychopy.org/u/kellyheom)\
**Post date:** [December 7, 2020, 3:03pm UTC](https://discourse.psychopy.org/t/abort-trial-if-mouse-stops-moving/18691/3 "2020-12-07T15:03:00Z")

</div>

Hello, thank you for your reply! Could you elaborate further? I think I understand (theoretically), but I’m not sure how to implement the part of moving the mouse subtracting from the counter.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [December 7, 2020, 3:13pm UTC](https://discourse.psychopy.org/t/abort-trial-if-mouse-stops-moving/18691/4 "2020-12-07T15:13:10Z")

</div>

Get the position of the mouse each frame. If it’s the same as the previous frame add 1 to a counter. If it’s different, reset the counter to 0

End the routine if the counter reaches a certain value

---

<div class="post-metadata">

**Author:** ![kellyheom](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kellyheom](https://discourse.psychopy.org/u/kellyheom)\
**Post date:** [December 11, 2020, 6:09pm UTC](https://discourse.psychopy.org/t/abort-trial-if-mouse-stops-moving/18691/5 "2020-12-11T18:09:33Z")

</div>

I figured this out by doing this:  
At begin routine

```auto
x_coord = []
y_coord = []
counter = 0

```

And then at each frame

```auto
x_coord.append(mouse.getPos()[0])
y_coord.append(mouse.getPos()[1])

if trial_clock.getTime() > 2:
    if (x_coord[-1] == x_coord[-2]) & (y_coord[-1] == y_coord[-2]) and buttons==[0,0,0]:
        counter = counter + 1
        if counter > 5:
            abort = 1
            continueRoutine = False
        else:
            continueRoutine = True

```

Thank you all for your help!
