| Reference | Downloads | Github

Code component to count mouse clicks

I clicked on your link: “No information available for this experiment: it may not exist or you may not have access to it.”
Could you make it public please? I hope I will be able to compare both codes to see if there is something wrong in mine.

Whoops, forgot to save changes. You should be able to access it now.

Yes, thank you.

@jonathan.kominsky ok it works now!! I had to select “new clicks only” in the mouse properties. Thank you so much!

Unfortunately it does not store any data about the component on which was clicked on. Do you know how I can add this to the code?

That requires creating a trialHandler.addData call that add this information to the output. That’s probably best done modifying the conditional statement that sets continueRoutine = false to something like this:

 if (gotValidClick === true) { // end routine on response
        psychoJS.experiment.addData('cigaretteImCounter', cigaretteImCounter);
        psychoJS.experiment.addData('mmsImCounter', mmsImCounter);
        continueRoutine = false;

That should add columns to your data recording the counters for the number of times each thing was clicked at the end of the trial.

Yes it did add the columns I need. Thank you very much for your support.

What if I want to get the reaction time (for each time a click is made) ?? Also, if you put the mouse.getPressed() in each fram tab, it logs more than 1 clicks since it runs every frame. So, how do you count the mouse clicks???

The code in this post (Code component to count mouse clicks) and PsyLing’s post above it accounts for this by checking whether that click was already in process as of the previous frame, in this exceprt:

mouse.time.push(mouse.mouseClock.getTime()); // get mouse time, for storage that is not implemented
if (!buttons.every( (e,i,) => (e == prevButtonState[i]) )) { // button state changed?
  prevButtonState = buttons; //button state as of last frame, makes sure holding mouse down has not affected anything
  //debug code
  //console.log('new button state detected');
  if (buttons.reduce( (e, acc) => (e+acc) ) > 0) { // state changed to a new click
    // rest of code checks if mouse is inside clickable objects and increments counters

In short, if the mouse button was down on the previous frame, then this condition is false, and it doesn’t increment the counters.

RT can be pulled from the mouse.time array, which updates on every frame. You could just pull the most recent element of mouse.time on a valid new click, though I’m not sure what that RT is matched against. Alternatively you could just use a util.Clock to get a reading at the start time and when this condition is met, and store that as your RT.

1 Like