psychopy.org | Reference | Downloads | Github

Mouse component: left or right button press

Hi All!
I’m trying to program a Posner task where stimuli pops up either on the left or right of the screen and in response the participant has to click either the right or left mouse button (not the stimuli)

PsychoPy version (e.g. 1.84.x): 2021.2.3
What are you trying to achieve?:
The mouse responses (resp1) are stored as resp1.leftbutton and resp1.right button where if the stored answers are 0 or 1’s under each column. I would like it to show up as one response as either 1 (left) or 2 (right) so that I can compare it to the corrAns set up in the conditions sheet.

mouse.component

What did you try to make it work?:
I believe I have to use the getPressed attribute but I am not sure how to use it.

mouse = resp1.getPressed()
if resp1.getPressed() == corrAns:
thisExp.addData(‘correct’, ‘Yes’)
else:
thisExp.addData(‘correct’, ‘No’)

Thank you very much!!

Dear Emily_J_Furtado,

you need some. Add a code-componente in the routine where you register mouse-click. In the begin tab of the code-component add the following:

correct = 0
button = []

In the end routine tab of the code-component add the following:

buttons = mouse.getPressed()

if (buttons[0] == 1 and corrAns == 1):
    correct = 1
elif (buttons[2] == 1 and corrAns == 2):
    correct = 1
else:
    correct = 0
    
thisExp.addData("correct", correct)

This add a column correct to your data file indicating whether the answer was correct (1) or incorrect (0). I prefer 0/1 instead of Yes/No for correct/incorrect answers because it allows to compute directly the accuracy. Anyway, change the code to your needs.

#store mouse clicks in buttons
#buttons[0] is 1 when the left mouse button was clicked and 0 otherwise
#buttons[2] is 1 when the right mouse button was clicked and 0 otherwise

You might have to change the name of the mouse-component. I use the default-name mouse.

Best wishes Jens
BTW delete the empty row in your excel-file. It might give you problems when running the experiment online.

Hi Jens,

This was super helpful!
Just a follow up question. Is there a way for me to create a percentage based on the “correct” variable ?
At the end of the experiment we would like the participants to receive a message that says “you got xx% percent right”

I added a routine at the end called “great_job” with a code component.

In begin routine, I added the following but nothing popped up at the end and the error says “Key error: correct”

correct = thisExp[‘correct’]
print(“correct: “, correct)
meanCorr = round(((correct)*100 / 10))
if float(meanCorr) >= 80:
outcome_message = f”{meanCorr}% correct. Good job!”
else:
outcome_message = f"{meanCorr}% correct. Try again!"

Any insight? Thank you very much

Best,
Emily!

Hello,

ok, add the following variables to your code-component in which you initialize your variables:

ncorrect = 0
idx = 0
msg = 0
qualifiedmsg  = "ToBeChanged"

Change the code in the end-routine tab which counts the correct mouse-clicks:

buttons = mouse.getPressed()

if (buttons[0] == 1 and corrAns == 1):
    correct = 1
    ncorrect += 1
elif (buttons[2] == 1 and corrAns == 2):
    correct = 1
    ncorrect += 1
else:
    correct = 0

idx += 1
thisExp.addData("correct", correct)

Add a code-component to the routine in which you display percentage correct. Make it the first component of the routine. Insert the following code in the Begin tab:

if (msg > 80):
    qualifiedmsg = "Great job!"
else:
    qualifiedmsg = "Try again!"

Add a text component similar to the following:

grafik

Best wishes Jens

Hi Jens,

Thank you so much for your time!

I did just as you said and although I went through the task and got all the trials correct it said that I had scored 0%. Do you know why this might be?

Best,
Emily

Hello Emily,

I guess it is do to the fact that I forgot to post a code line that you have to add to the code-component in which you display the percentage correct.

msg = round(ncorrect/idx*100)

if (msg > 80):
    qualifiedmsg = "Great job!"
else:
    qualifiedmsg = "Try again!"

Sorry for the copy and paste error.

Best wishes Jens

Hi Jens,

That worked perfectly! Thank you very very much for all your time and help

Best,
Emily