Code component to count mouse clicks

OK. So. I decided to actually have a look at this and have learned some things.

The corsi demo no longer works because the mouse never receives the status “STARTED” (@jon, unintended behavior? see https://github.com/psychopy/psychopy/issues/2844).

There is a way to make your task work. It requires much more code than it probably should, but I have tested this and know it works. Notably, this is direct JS code, we are not going to be going through python. Here’s how to do it.

First, in “begin routine”, place the following:

mouse.x = [];
mouse.y = [];
mouse.leftButton = [];
mouse.midButton = [];
mouse.rightButton = [];
mouse.time=[];
prevButtonState = mouse.getPressed();
mmsImCounter=0;
cigaretteImCounter=0;

Then, in every frame, the following:

let buttons = mouse.getPressed(); // read mouse state
const xys = mouse.getPos(); // get mouse coordinates
mouse.x.push(xys[0]); // add mouse coordinates to x/y list, in principle for data storage, but not implemented right now
mouse.y.push(xys[1]);
mouse.leftButton.push(buttons[0]); // store buttons in button list, likewise for storage
mouse.midButton.push(buttons[1]);
mouse.rightButton.push(buttons[2]);
//debug code
//console.log('x location: ' + String(mouse.x) + ' y location: ' + String(mouse.y));

mouse.time.push(mouse.mouseClock.getTime()); // get mouse time, again 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
    // check if the mouse was inside our 'clickable' objects
    gotValidClick = false;
    if (cigaretteIm.contains(mouse)) {
        cigaretteImCounter++;
        if (cigaretteImCounter >= 5) {gotValidClick = true;}
    } else if (mmsIm.contains(mouse)) {
          mmsImCounter++;
          if (mmsImCounter >= 5) {gotValidClick = true;}
    }
    if (gotValidClick === true) { // abort routine on response
        continueRoutine = false;
    }
  }
}

I am virtually certain that will work, but it may not store data from each click at the end of the trial, just the final click (and even that may be kind of iffy). However, I have verified that it will end the trial correctly when you click on one of the things five times.

Additional code will be required to save the complete mouse movement, if that’s data you want.