# Save mouse click on polygon (in relation to left - middle - right)

**URL:** <https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421>\
**Category:** Coding\
**Tags:** screen, mouse, code\
**Created:** [December 9, 2021, 6:15pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421 "2021-12-09T18:15:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnnaPy](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@AnnaPy](https://discourse.psychopy.org/u/AnnaPy)\
**Post date:** [December 9, 2021, 6:15pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/1 "2021-12-09T18:15:49Z")

</div>

Hey everyone,

I am currently building a line bisection task with the PsychoPy Builder.  
Now I wanna add some code, so that in the end there will be a column in my excel sheet, that tells me where, compared to the stimuli (a rectangular polygon, thin as a line), the person clicked with the mouse.

Goal is to find out if the person clicked too far to the left or too far to the right of the polygon.  
Up till now the mouse response is only saved in relation to the screen-size.

If you know any way to code this or have tipps for pieces of code I could use, I’d be very thankful!

---

<div class="post-metadata">

**Author:** ![B.D.5](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@B.D.5](https://discourse.psychopy.org/u/B.D.5)\
**Post date:** [December 9, 2021, 10:18pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/2 "2021-12-09T22:18:03Z")

</div>

I do not know if that makes sense but psychopy records the mouse position, and you have a polygon position. you might write a code that subtract these two variables. Or ypu öay consider calculatibg in the cond file

---

<div class="post-metadata">

**Author:** ![AnnaPy](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@AnnaPy](https://discourse.psychopy.org/u/AnnaPy)\
**Post date:** [December 9, 2021, 11:31pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/3 "2021-12-09T23:31:32Z")

</div>

Makes sense to me, but do you know which Code I should use for that?  
I found the thisExp.addData, but I don’t know if it is the right one…

---

<div class="post-metadata">

**Author:** ![B.D.5](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@B.D.5](https://discourse.psychopy.org/u/B.D.5)\
**Post date:** [December 10, 2021, 6:49am UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/4 "2021-12-10T06:49:17Z")

</div>

I do not want to mislead you but, I guess you should run the experiment first.

Check the csv data file. You will see the variable names for X axis of the mouse e.g. Mouse.x mouse.y Since you are interesed in right or left, it should be x axis I think.  
Did you define positions for polygon and line components in the condition xlxs file? I mean do you have a column for it? If yes, lets say the name is line\_posx

Begin routine:

```auto
new_pos = mouse.x - line_posx #this is just for demonstration purposes you should decide which one to subtract first

if new_pos <0: #we mean that mouse position is less than the line position. So it is on the left 
    new_pos = 'left'
    thisExp.addData ('leftorright', new_pos)
if new_pos > 0 :
    new_pos = 'right'
    thisExp.addData ('leftorright', new_pos)

if new_pos == 0 :
    new_pos = 'middle'
    thisExp.addData ('leftorright', new_pos)
Someone can correct me if I am wrong.

```

---

<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 10, 2021, 11:28am UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/5 "2021-12-10T11:28:30Z")

</div>

Very close, but with some unnecessary repetition. How about

```auto
new_pos = mouse.getPos()[0] - line_posx #this is just for demonstration purposes you should decide which one to subtract first

if new_pos < 0: #we mean that mouse position is less than the line position. So it is on the left 
    new_pos = 'left'
elif new_pos > 0 :
    new_pos = 'right'
else:
    new_pos = 'middle'
thisExp.addData ('leftorright', new_pos)

```

---

<div class="post-metadata">

**Author:** ![AnnaPy](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@AnnaPy](https://discourse.psychopy.org/u/AnnaPy)\
**Post date:** [December 10, 2021, 1:16pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/6 "2021-12-10T13:16:05Z")

</div>

Thanks for all the answers, I will try them out!

Is there also a possibility to save the exact deviation from the middle? For example, the subject deviated 0.2 degrees/cm/whatever measure from the middle to the right/left.

---

<div class="post-metadata">

**Author:** ![B.D.5](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@B.D.5](https://discourse.psychopy.org/u/B.D.5)\
**Post date:** [December 10, 2021, 1:34pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/7 "2021-12-10T13:34:29Z")

</div>

Do you mean from the center of the screen or from the position of polygon or line component?

---

<div class="post-metadata">

**Author:** ![AnnaPy](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@AnnaPy](https://discourse.psychopy.org/u/AnnaPy)\
**Post date:** [December 10, 2021, 1:46pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/8 "2021-12-10T13:46:09Z")

</div>

I mean the polygon.  
I already get the exact position of the mouse click compared to the screen. But I need to know it in relation to the polygon.

---

<div class="post-metadata">

**Author:** ![B.D.5](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@B.D.5](https://discourse.psychopy.org/u/B.D.5)\
**Post date:** [December 10, 2021, 1:52pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/9 "2021-12-10T13:52:17Z")

</div>

> [@AnnaPy](#):
>
> I already get the exact position of the mouse click compared to the screen. But I need to know it in relation to the polygon.

I guess the difference we define already is the deviation from the polygon.

```auto
new_pos = mouse.getPos()[0] - line_posx 
if new_pos == 0.2: #if 0.2 away from the polygon
    deviationfrompolygon= 0.2
thisExp.addData ('deviationfrompolygon', deviationfrompolygon)

```

---

<div class="post-metadata">

**Author:** ![AnnaPy](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@AnnaPy](https://discourse.psychopy.org/u/AnnaPy)\
**Post date:** [December 10, 2021, 2:23pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/10 "2021-12-10T14:23:00Z")

</div>

But what if I don’t wanna define the distance. So no matter how far away from the middle a person clicks, in the end I will get the distance from the middle of the polygon.

In your example, I will only get the info for 0.2, right?

---

<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 10, 2021, 2:24pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/11 "2021-12-10T14:24:14Z")

</div>

Our previous suggestions don’t record the distance.

---

<div class="post-metadata">

**Author:** ![AnnaPy](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@AnnaPy](https://discourse.psychopy.org/u/AnnaPy)\
**Post date:** [December 10, 2021, 2:53pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/12 "2021-12-10T14:53:43Z")

</div>

Yes, I know. But that is what I’d like to obtain.  
So it is important to know if the person clicked right or left from the middle, but it is also important to know how far away from the middle of the polygon he or she clicked.

Also, because I don’t know if this is clear: the polygon is not always in the middle of the screen.

---

<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 10, 2021, 10:26pm UTC](https://discourse.psychopy.org/t/save-mouse-click-on-polygon-in-relation-to-left-middle-right/26421/13 "2021-12-10T22:26:11Z")

</div>

What are the coordinates of the polygon?

Is your mouse component called mouse?
