psychopy.org | Reference | Downloads | Github

Using the mouse for selecting images

OS (e.g. Win10): Win8
PsychoPy version (e.g. 1.84.x): 1.83.04
Standard Standalone? (y/n) If not then what?: Yes

What are you trying to achieve?:
I am trying to put two pictures ( one on the left and one on the right) and when I want to select either of them, I would put the mouse pointer on one image ( the one I would select) and then click on it instead of using the keyboard (eg., left/right keys) .

I have got an idea that I use the Rating Scale with two choices (hover) and I put each choice under each picture where I can put the mouse pointer on the choice under the image. However, the problem here is that I couldn’t put long space between choices.

So I would be thankful if someone could help me with this.

Best regards,
Sami

1 Like

Hi Sami,

You need to tell us what you want the selecting to do. i.e. does this provide the answer to a question, does it end the trial, does it make one of the images appear highlighted, etc.

But in essence, no you don’t want to use a rating scale. Instead, you would insert a mouse component and also a code component. In the Each Frame tab of the code component, you would have something like this:

if mouse.isPressedIn(name_of_your_left_stimulus):
    # do something 
elif mouse.isPressedIn(name_of_your_right_stimulus):
    # do something else

Michael

2 Likes

Thank you so much Michael for your great answer.

Yes, I want the selecting to end the trial.And I want the output data file shows if a participant has selected the left or right image.
The name of my left stimulus is $pictureleft. In this case, Shall I insert the dollar in brackets? E.g., if mouse.isPressedIn ( $pictureleft): …
I have got a list of images in the reference file.

Also, could you please provide me with the full code because I am not familiar with coding?

Many thanks
Sami

When adding code in a code component, never use a $. (The $ is not standard python. Its needed to signal the Builder that a string should be interpreted as python code. Code components are always python code, but other builder entry fields are ambiguous, and so a special character is needed.)

if mouse.isPressedIn(pictureleft):
    continueRoutine = False
    # save data if you like:
    thisExp.addData('clicked', 'left')
elif mouse.isPressedIn(pictureright):
    continueRoutine = False
    # save data if you like:
    thisExp.addData('clicked', 'right')

Hi Jermey,

Thank you so much for providing the coding.

I used the one you put, but an error message has appeared. The message has been attached.
When I remove the coding, the experiment works.
Could you please have a look at the message if you could see what the problem is?

Many thanks
Sami

That’s probably because pictureLeft is a (unicode) string with the image file? You want to refer to the image object:

if mouse.isPressedIn(image_3):

Just to iterate on @jeremygray’s answer: If you only care about whether the subject responded left or right, don’t refer to the content of the image stimulus at all; simply refer to the image stimulus object itself:

if mouse_4.isPressedIn(image_3):
    continueRoutine = False
    # save data if you like:
    thisExp.addData('clicked', pictureLeft)
elif mouse_4.isPressedIn(image_4):
    continueRoutine = False
    # save data if you like:
    thisExp.addData('clicked', pictureRight)

I changed the logging so that it saves the image name.

That’s great, Lindeloev,

It works, but when I click on either of the two pictures in the first trial, the experiment finishes; that is, the next trial ( the next two pictures) does not come.

Thank you in advance for your help

One guess is that it actually loops through all the trials for the duration of your button press. In that case, your data file should have a row for each of the trials while, if it really did quit early, it should just have a row for the first trial or none at all. Let me know if you don’t have the three rows.

Assuming that you have, fix the problem by either of these:

  1. Insert a “break” routine after your main routine, e.g. with a 0.3 second blank screen.
  2. Wait for mouse release by adding this to your new “wait-routine” in a code component instead of a time-based wait:
while np.any(mouse_4.getPressed()):
    pass

… or you could add this code under the if-statements in my code example above.

Thank you so much :+1: We are very close to the compete solution.

I put the new code under the previous one and it successfully worked. However,
The output data file doesn’t show if a participant’s response is correct or not. For example, when he chooses the correct picture, this should be marked as ‘1’ or ‘correct’ and vice versa.

Many thanks for the help
Sami

You would have a column in your conditions file specifying something that identifies the correct image. Let’s say that you call that column corrAns and that it specifies the correct image by filename:

if mouse_4.isPressedIn(image_3):
    continueRoutine = False
    # save data if you like:
    thisExp.addData('clicked', pictureLeft)
    thisExp.addData('score', int(corrAns == pictureLeft))
elif mouse_4.isPressedIn(image_4):
    continueRoutine = False
    # save data if you like:
    thisExp.addData('clicked', pictureRight)
    thisExp.addData('score', int(corrAns == pictureRight))

The == tests for equality and returns True or False. int converts these into 1 and 0 respectively.

You could also use the side of the correct image as an identifyer, e.g. having the corrAns column say ‘left, right, right, left’ etc. Then you would write that string in the test for equality:

if mouse_4.isPressedIn(image_3):
    continueRoutine = False
    # save data if you like:
    thisExp.addData('clicked', pictureLeft)
    thisExp.addData('score', int(corrAns == 'left'))

Thank you so much Lindeloev for your great answer. The problem has been completely solved.

Many thanks again
Sami

The topic looks very interesting. But what seems to me is that the mouse pointer will respond ( taking the participant to the next trial) wherever it is clicked on the screen, I don’t know if you can make the mouse responds ( going to the next trial) only when either of the two pictures is clicked.

Also, it would be perfect if you could make an image appear highlighted ( or make the cursor hand on mouse over) when the mouse is put on the image before clicking on it.

Good luck with your experiment

Yuu

@Yuu, it will only continue if you have “force end of routine” ticked in your mouse component. Otherwise not since continueRoutine = False only runs when the click is on an image in the above code. To make the image appear highlighted, you could either change it’s properties when a click is detected:

image_4.contrast = 0.5
image_4.opacity = 0.5
image_4.color = 'red'

making sure to reset these parameters before the next trial, or you could draw a ShapeStim above it.

Thank you Yuu. Yes, as Lindeloev said the ‘force end of routine’ should be inactive.

hi, I have a quite similar question, so I decided to include it here instead of opening a new discussion.
In my case, instead of pictures, I have polygon components (they are 16 placed as a 4x4 grid, but to make it simple, lets consider there are only two, q1 on the left side, and q2 on the right side). In my conditions file I have a column (corrAns) specifying the target polygon (the one that will be considered correct in each trial.

I tryed to follow the previous answers, but I dont get any information about mouse response positions in my output file

so here is my code component

if myMouse.isPressedIn(corrAns):
    continueRoutine = False
    myExp.addData('clicked', q1_3)
    myExp.addData('score', int(corrAns == q1_3))
elif myMouse.isPressedIn(q2_3):
    continueRoutine = False
    myExp.addData('score', q2_3)
    myExp.addData('acerto', int(corrAns == q2_3))

many thanks in advance
Best,
Ricardo

As with the answer to the previous question you need to make sure that your variables for which you test isPressedIn() (corrAns and q2_3 in your case) are in fact stimuli like image stimuli. corrAns is a variable that is usually defined in a conditions file not a Component of your experiment.

Hi Jon , thank you.
Sorry, it was my mistake when I pasted the code here. The correct thing is myMouse.isPressedIn(q1_3), as reproduced below.

if myMouse.isPressedIn(q1_3):
    continueRoutine = False
    myExp.addData('clicked', q1_3)
    myExp.addData('score', int(corrAns == q1_3))
elif myMouse.isPressedIn(q2_3):
    continueRoutine = False
    myExp.addData('clicked', q2_3)
    myExp.addData('score', int(corrAns == q2_3))

My question is if it possible to refer to polygons in the isPressedIn(). But as I dont get anything in my output csv file (no sign of the “clicked” and score" columns), I am afraid there are other things I am not doing correctly.

Best regards
Ricardo

Dear all
This has been a very useful chain of conversation and very relevant to me. I think my question will fit here.
conditions.xlsx (10.4 KB)
I am starting to design one of my very simple experiment using psychopy builder view (I have used this before, so even though I am not good in programming I can understand simple codes and implement them by ‘add custom code’).
Here is what I need:

  1. I have six pictures appearing on screen until one of them is touched. I understand if I design it using mouse click (right by default), I can manage to make it work in touch screen.
  2. I have added a ‘Conditions’ file in my loop. The ‘condition.xlsx’ file has seven columns, i.e., on every trial the picture from the first row is displayed until a picture is selected as a response. The eighth column of the ‘condition.xlsx’ is the corrAns stating which picture is the correct response for that particular trial.
  3. I have set up ‘Mouse’ as a response mode and now it correctly runs through each row and displays the trials properly.

My question…

How do I write my ‘code’ such that if the ‘corrAns’ is touched it gives a score of ‘1’ else it gives the score of ‘0’. I want the data sheet to record the responses as ‘1’ or ‘0’. Note, the trial ends irrespective of accuracy of the response and no feedback is required. And also can the program print the ‘total response correct’ after the completion of the task. Lets say the task has 30 trials.

1 Like

Ricardo,
The issue Jon mentioned is still an issue for the addData() commands, int(corrAns == q1_3).

The value of corrAns will come from your conditions file, and its type will be string or int probably. The value of q1_3 is an image or other container, which is definitely not of type string or int. So the conditional expression will always be False, because of this type mismatch.

Think through what will work for you. One possibility might be to have the corrAns column contain strings of the form “q1_3”. Then your conditional expression could be (for the q1_3 part): int(corrAns == "q1_3") – note the quotes here.

Hi Jeremy , could you answer my question please.
Looking forward to hearing your repsonse to it.