# Measuring response time with mouse component

**URL:** <https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236>\
**Category:** Builder\
**Created:** [December 10, 2019, 9:02am UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236 "2019-12-10T09:02:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![lilly](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@lilly](https://discourse.psychopy.org/u/lilly)\
**Post date:** [December 10, 2019, 9:02am UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/1 "2019-12-10T09:02:44Z")

</div>

**OS** (e.g. Win10): macOS 10.5  
**PsychoPy version** (e.g. 1.84.x): 3.1.5  
**Standard Standalone? (y/n)** Yes  
**What are you trying to achieve?:** I want to measure the response time of the participant clicking a `visual.ButtonStim`.

**What did you try to make it work?:** I tried using the mouse component and putting the name of the button stim into the ‘clickable stimuli’ field in the mouse component.

**What specifically went wrong when you tried that?:** Mouse.time in the output measures the time of any mouse click on the screen, not only on my button stim (which is what I want to achieve).

I am new to PsychoPy and I would be very grateful for any suggestions on how I can achieve this or any insights on what I might not have understood correctly.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [December 10, 2019, 10:38am UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/2 "2019-12-10T10:38:49Z")

</div>

Hi @lilly, have you selected to end the routine on “valid click” and set the mouse save state to “final”?

---

<div class="post-metadata">

**Author:** ![lilly](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@lilly](https://discourse.psychopy.org/u/lilly)\
**Post date:** [December 10, 2019, 10:48am UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/3 "2019-12-10T10:48:18Z")

</div>

Hi @dvbridges, thank you for your reply. I have not selected to end the routine on “valid click” because this is not supposed to happen as the participants have a certain amount of time for this routine and should be able to change their decision during that time. Is there a way to store the response time of the last click on the button?

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [December 10, 2019, 10:56am UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/4 "2019-12-10T10:56:21Z")

</div>

If you want the mouse response saving on the button click, you can use the following code in the Each Frame tab of a code component:

```auto
if <yourButton>.buttonClicked():
    thisExp.addData("mouse.trial_time", t) # Save time relative to start of trial
    thisExp.addData("mouse.mouse_time", mouse.mouseClock.getTime()) # Save time relative to start of mouse
    continueRoutine = False

```

---

<div class="post-metadata">

**Author:** ![lilly](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@lilly](https://discourse.psychopy.org/u/lilly)\
**Post date:** [December 10, 2019, 11:08am UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/5 "2019-12-10T11:08:06Z")

</div>

I tried to do this but I got this error message:

```auto
 if ButtonLeft.buttonClicked():
TypeError: buttonClicked() missing 1 required positional argument: 'mouse'

```

Just to be clear what I want to achieve: In this routine, I have two button stims called ButtonLeft and ButtonRight. Participants have eight seconds to click any one of those buttons or press one of them and then change their decision and click the other button. The last button press in this routine is the one that I am interested in as the button that is selected at the end of the routine is the participant’s valid decision.  
I appreciate your help!

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [December 10, 2019, 12:01pm UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/6 "2019-12-10T12:01:08Z")

</div>

Ok, well what you can do is:

- set a time attribute to each button
- Update time attributes with trial time on each screen refresh
- Set buttons as clickable stimuli
- Store params for clicked stimuli i…e, name and new time attribute
- Save the last stim that was clicked.

The code to do this is:

```auto
# Begin Routine
leftButton.time = 0
rightButton.time = 0

# Each Frame
leftButton.time = t
rightButton.time = t

# End Routine
if len(mouse.clicked_name):
    thisExp.addData("clickedButtonName", mouse.clicked_name[-1])
    thisExp.addData("clickedButtonTime", mouse.clicked_time[-1])
else:
    thisExp.addData("clickedButtonName", None)
    thisExp.addData("clickedButtonTime", None)

```

Mouse component will look like this:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/0/1/0198ab2ed6fa6e9b08e557c2b7c18b556d705be7.png)

---

<div class="post-metadata">

**Author:** ![lilly](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@lilly](https://discourse.psychopy.org/u/lilly)\
**Post date:** [December 10, 2019, 3:00pm UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/7 "2019-12-10T15:00:57Z")

</div>

I tried following your steps but I don’t get any data in my output file.

![Ohne Titel](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/b/c/bc743e53154d14a96b5dca159604171182cd7ab0.png)

![Ohne Titel Kopie](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/6/c/6c118f8141d75d54e514a859a99797e3b80eabfa.png)

I carefully followed your steps but maybe there is something missing?

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [December 10, 2019, 4:41pm UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/8 "2019-12-10T16:41:12Z")

</div>

Ok, did you change the names of the buttons in the code? e.g., `leftButton` should become `ButtonLeft`, and same for the mouse component

---

<div class="post-metadata">

**Author:** ![lilly](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@lilly](https://discourse.psychopy.org/u/lilly)\
**Post date:** [December 11, 2019, 7:53am UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/9 "2019-12-11T07:53:13Z")

</div>

Yes, I adapted all of the names to match mine.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [December 11, 2019, 11:24am UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/10 "2019-12-11T11:24:36Z")

</div>

Ok, would you mind sharing the task to I can take a look? You could upload to Pavlovia and share the link to the project.

---

<div class="post-metadata">

**Author:** ![atprabhakar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/atprabhakar/32/32535_2.png) [@atprabhakar](https://discourse.psychopy.org/u/atprabhakar)\
**Post date:** [January 27, 2022, 3:21am UTC](https://discourse.psychopy.org/t/measuring-response-time-with-mouse-component/10236/11 "2022-01-27T03:21:28Z")

</div>

Thank you for this. It works very well
