Mouse Click as Event trigger in Psychopy 1.90.3

Hey everybody,

in my trial loop I am trying to initialize the presentation of a visual.ImageStim (a simple .jpg) by a mouse-click, so as soon as the left mouse button is clicked, i want to draw my stimulus. It worked out fine with a keypress, but I did not manage to set the mouse click as the event trigger. In my code I tried out this:

mouse = event.Mouse()
mouse_click = mouse.getPressed()

def doTrial (stimuli_per_block):
    for x in range (len(stimuli_per_block)):
        event.clearEvents()
        if mouse_click == (0,0,0):
            core.wait()
        elif mouse_click == (1,0,0):
            stimuli_per_block[x].draw()

I thought by setting my variable mouse_click as mouse.getPressed(), this variable always contains the tuple of 3 elements, indicating if a button of the mouse was pressed, so I thought I could use this as a condition prior to my visual stimulus, but it does not work out. Has anyone experience with that problem in Psychopy version 1.90.3 (Python 2.7)?

Thanks for your help!

Carrie

Hi Carrie, we need to see the whitespace in your code:

But there do seem to be several sequencing errors apparent with your code (e.g. the mouse is checked only once, outside your function, which runs once per trial). Could you post the code you have with the keypress that does work? It will be very easy to modify that as required.

Hey thanks for the remark Jon, I hope my post is now according to the guidelines of the forum.

@Michael: Thanks for your answer, I wanted to rebuild the event.waitKeys function with a mouse click as the event to wait for. But I think I misunderstood what the function mouse.getPressed() is doing. I changed my code now, so that the check for the mouse is within the loop (see attached snippet). Now the mouse click actually works, but the program will not wait for the mouse event, so I need some code to tell the program that it should wait until the click happens. Maybe there is a easy way to solve this, but I haven’t found a solution yet.

# define trial run 

def doTrial(stimuli_per_block):

    for x in range(len(stimuli_per_block)):
        
        wait.draw() # show instruction to wait for stimuli to appear
        win.flip()
        
        if mouse.getPressed()[0]==1: # if the left mouse button is pressed
            
            stimuli_per_block[x].draw() # draw stimulus from randomized list
            win.flip()
            core.wait(2) # show stimulus for 2 seconds
            win.flip()
            core.wait(3) # wait 3 seconds until next trial run starts
            trial_clock.reset
            
        mouse.clickReset()

OK, you’re getting close. Could you please now just precisely describe what the sequence of events should be in a trial (ie from the participant’s perspective, don’t worry about the code side).

Okay: every trial should start with a instruction to press a key(and hold it pressed) and to look the conductor in the eyes, as soon as they are ready (this instruction is my wait-stimulus, which is a simple visual.TextStim). So this should be visible until the participant establishes eye contact. If this is happening, the conductor clicks the mouse to initialize the stimulus presentation. The participant is then suppose to nonverbally indicate with a pointing gesture and gaze on which side a target appears. I want to measure the time of the key release and start of the saccade in relation to the stimulus presentation (maybe it is important to mention that I want to implement this paradigm with pyLink for eyeLink 1000 control). The stimulus should disappear after 2 seconds and I want to include additional 3 seconds buffer time with no event or visual until the next trail and the instruction appearing again. So as it is dependent on the participant when eye contact is established, I need some wait-function to keep the program running until the mouse click and the proceeding of the loop. I hope that was understandable :slight_smile:

Just one thing: you talk about both a key-release and a mouse press (by the conductor) that starts the trial. The key-release isn’t fully described as to who does it and when, though?

Of note, PsychoPy’s current keyboard handling doesn’t work in the same way as the mouse: with the latter, we can detect when a button is being held down (i.e. the current state of the mouse button), but for keys, we only get the initial keypress event, so we can’t (as easily) detected key releases. A new option for keyboard handling (to come out in version 3.1) will address this issue, but for now, perhaps just describe whether a keyboard release event is actually what you are after (and when it should occur).

The mouse click is started by the conductor, the key-release is done by the participant. Participants are instructed to press the key with their index finger at the start of every trial and keep it pressed until stimulus presentation. As soon as the stimuli appear (initialized by the mouse click of the conductor), participants use the finger for pointing to the target (time of key-release).

I want to record the time of the key-release in relation to the stimulus presentation and gaze data (I use the eyelink 1000). My current plan is to send a message to my tracker with the time of the stimulus presentation ((pylink.sendMessageToFile() - https://www.psychopy.org/api/hardware/pylink.html#pylink.sendMessageToFile). The tracker itself offers the possibility to record button status of up to 8 buttons on the keyboard. I want to record the key-release by configuring the initialization file of my eye-tracker. So I don’t have to implement that by psychopy-code.

Okay, I was a Little bit confused but now I managed to solve the Problem by simply wrapping my instruction TextStim in a while-loop. So for others who want to implement mouse clicks as Event trigger, Maybe my snippet might be helpful:

def doTrial(stimuli_per_block):

    for x in range(len(stimuli_per_block)):
        
        while mouse.getPressed()[0]==0:
        
            wait.draw()
            win.flip()
        
        if mouse.getPressed()[0]==1:
            
            stimuli_per_block[x].draw() # draw stimulus fron randomized list
            win.flip()
            core.wait(2) # show stimulus for 2 seconds
            win.flip()
            core.wait(3)
            trial_clock.reset
            
        mouse.clickReset()

Thanks @Michael for your help!