| Reference | Downloads | Github

Movement Detection Box

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.

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

“Begin routine” tab:

previous_position = mouse.getPos()

“Each frame”: tab:

current_position = mouse.getPos()

if current_position == previous_position:
    your_stimulus.fillColor = 'black'
    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.


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.

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

‘Begin Routine’

x_pre,y_pre = mouse.getPos()

‘Each Frame’

x_curr,y_curr = mouse.getPos()

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

x_pre = x_curr

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

Thank you again.