Move forward and backward through slides using Mouse clicks on Left and Right arrows

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2022.2.4
Standard Standalone? (y/n) yes
What are you trying to achieve?:
I followed this youtube tutorial from Rebecca making slides that can move forward and backward.

What did you try to make it work?:
I want to make the move forward and backward slides responding to mouse clicks on Left and right Arrows instead of keyboard responses as the video shows.

What specifically went wrong when you tried that?:
The tutorial set a loop with nReps = 100 (a large num) and uploaded (say) Slide1 to Slide6 as image, and make the image path $‘Stimuli/Slide’ + str(slideN) + ‘.JPG’. and created a keyboard response

I coded in PsychoJS

slideN = 1;
maxSlideN = 6;
minSlideN = 1;
if ((key_resp.keys == 'left')) {
    slideN -= 1;
} else {
    if ((key_resp.keys == 'right')) {
        slideN += 1;
    }
}

It worked perfectly fine in responding to keyboard press, however, I want to make the slides respond to mouse clicks to left arrow and right arrow.

I uploaded leftArrow and rightArrow images, and disabled the keyboard and added mouse. I coded

if ((coord_mouse.isPressedIn(leftArrow))) {
    slideN -= 1;
    console.log(slideN);
} else {
    if ((coord_mouse.isPressedIn(rightArrow))) {
        slideN += 1;
        console.log(slideN);
    }
}

According to console message, slideN updates but the slides didn’t move forward/backward. I think the problem is that I set Keyboard responds to every repeat, so the loop responds to one keypress as one trial and update the slideImage. But “every repeat” is not an option for mouse clicks, it stays in one trial. That’s my guess. Is there any other way that I can make it work? Thanks so much if any help!

Include pasted full error message if possible. “That didn’t work” is not enough information.

I’m not quite sure what you mean by each repeat here. Are you setting the mouse to end the routine on a valid click and adding the two arrow image components as options?

I figured that when the mouse is pressed in RightArrow, slideN +=1 is executed so quickly and many times. It makes slideN from 1 to 5(max) in less than a second.

if (mouse.isPressedIn(rightArrow)) {
slideN +=1
}

is executing as long as the mouse is pressed down on the arrow and slideN quickly reaches the max. This is different from what I want to achieve. I want to implement that with one click, the slide is moving to the next slide. Is there any function that counts the number of clicks on the arrow? Thanks!

I think the reason keypress works as shown in the video is that Rebecca used a loop with a large number of trials (500) and created a key_resp which is set to respond “every repeat”. Therefore, in one Routine (repeat) key_resp listens to the valid keypress once and ends. It prevents slideN += 1 being constantly incrementing.

Keyboards can be set to register new presses only but mouse detection is done every frame. The way I normally deal with this is to add a minimum RT, to give time for the mouse button to be unpressed. e.g.

if mouse.isPressedIn(rightArrow) and t > .5:
     slideN +=1

The more sophisticated route would be to have a flag. The code would look something like:

if mouse.isPressedIn(rightArrow) and mouseDown == False:
     slideN +=1
     mouseDown = True
elif mouse.getPressed()[0] == False:
     mouseDown = False