# Movement Detection Box

**URL:** <https://discourse.psychopy.org/t/movement-detection-box/8821>\
**Category:** Builder\
**Created:** [August 19, 2019, 1:49am UTC](https://discourse.psychopy.org/t/movement-detection-box/8821 "2019-08-19T01:49:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![othsoul](https://avatars.discourse-cdn.com/v4/letter/o/c57346/32.png) [@othsoul](https://discourse.psychopy.org/u/othsoul)\
**Post date:** [August 19, 2019, 1:49am UTC](https://discourse.psychopy.org/t/movement-detection-box/8821/1 "2019-08-19T01:49:19Z")

</div>

**OS** (e.g. Win10): Win10/OSX  
**PsychoPy version** (e.g. 1.84.x): 3.1.2  
**Standard Standalone? (y/n)** If not then what?: yes  
**What are you trying to achieve?:**

I am simply trying to put a black box in the corner of trial screen that turns white everytime the cursor is moving.  
In other words, if I am moving the cursor mid-trial, it is white. if I stop the cursor it goes black.

I am not sure how difficult would be to implement but any tip is appreciated.

---

<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:** [August 19, 2019, 1:56am UTC](https://discourse.psychopy.org/t/movement-detection-box/8821/2 "2019-08-19T01:56:44Z")

</div>

Something like this in a custom code component (inserted from the “custom” component panel):

“Begin routine” tab:

```
previous_position = mouse.getPos()

```

“Each frame”: tab:

```auto
current_position = mouse.getPos()

if current_position == previous_position:
    your_stimulus.fillColor = 'black'
else:
    your_stimulus.fillColor = 'white'

previous_position = current_position

```

This assumes you have a mouse component simply called `mouse`. The code component should be below the mouse component, so the code gets access to the very latest mouse position.

The shape stimulus should have its fill colour field set to be “constant”, so that it doesn’t interfere with you over-riding it in real-time via code.

---

<div class="post-metadata">

**Author:** ![othsoul](https://avatars.discourse-cdn.com/v4/letter/o/c57346/32.png) [@othsoul](https://discourse.psychopy.org/u/othsoul)\
**Post date:** [August 20, 2019, 11:24pm UTC](https://discourse.psychopy.org/t/movement-detection-box/8821/3 "2019-08-20T23:24:11Z")

</div>

Michael,

thank you for your reply.  
I tried the code you suggested but I get this error:  
'AttributeError: ‘Mouse’ object has no attribute ‘pos’

I changed pos to getPos, while the program runs now, it doesn’t do as intended.

Any tips how I can fix it?

Thank you for your support.

Edit:  
thank you again for your code.  
I was able to fix the code by using coordinates. My code looks like that now:

‘Begin Routine’

```auto
x_pre,y_pre = mouse.getPos()

```

‘Each Frame’

```auto
x_curr,y_curr = mouse.getPos()

if x_curr == x_pre:
    polygon.fillColor = 'black'
else:
    polygon.fillColor = 'white'

x_pre = x_curr

```

It only detects horizontal movements, but I can extend it to detect both.

Thank you again.
