psychopy.org | Reference | Downloads | Github

Logging mouse click locations

Hello,

Using the code below, I’m trying to open a window for 10 seconds, have the user click anywhere on the screen as many times as they wish, and I want to log the mouse click location and time they clicked (RT). I didn’t code the RT time yet. Every time the user clicks within the window (i.e. myMouse.getPressed) occurs, I’m appending the click location to a list (mouse_click_locations[]). However, the list is being appended with the same click location many times, on every frame. I want to append the click location to the list once, and no more until another click is initiated. I thought that adding ‘myMouse.clickReset()’ at the end of each frame would do this, but it doesn’t make a difference.

After 10 seconds, I want the list to be populated with one location (x,y coord) for each mouse click initiated.

from psychopy import visual, core, gui, event, sound

win = visual.Window([800,600],color=(1,1,1), colorSpace='rgb', rgb=None, allowGUI=True, monitor='testMonitor', 
units='deg', fullscr=False)

myMouse = event.Mouse(visible=True,win=win)

refresh_rate = 60.0
default_time = 10

time_window = default_time * refresh_rate
time_window = int(time_window)

running = 1
while running:

    mouse_click_locations = []

    for frame in range(time_window):
    
        mouse_loc = myMouse.getPos()
        mouse_click = myMouse.getPressed()
    
        if mouse_click:
            mouse_click_locations.append(mouse_loc)

        win.flip()
        myMouse.clickReset()

    running = 0

win.close()

print mouse_click_locations

Could someone help me achieve this? am I using myMouse.clickReset() incorrectly?

Cheers,
Jon

Hi Jon,

.getPressed() is True for the duration of a mouse click, which will often last longer than one screen refresh. So you need to keep track of the first time you detect the mouse press, and then when it is no longer pressed. Only store the position and retain time on the first detection. After it is released, you start looking for a fresh press.

.clickReset() jus resets the timer associated with the mouse (for measuring reaction times). It doesn’t stop the mouse button being detected as down.

e.g. something like this (not tested):

mouse_down_detected = False

for frame in range(time_window):

    mouse_click = myMouse.getPressed()

    if mouse_click[0]: # button 0 is pressed
        if not mouse_down_detected: # initial detection
            mouse_loc = myMouse.getPos()
            mouse_click_locations.append(mouse_loc)
            mouse_down_detected = True
    else:
        mouse_down_detected = False

    some_stimuli.draw()
    win.flip()

1 Like

Thanks Michael, that’s very helpful.