Making Gabors Appear when Mouse is Clicked Anywhere on Screen

Hello everyone!

I am reaching out for help on how to make shapes appear by clicking the mouse.

I previously got help making this happen while clicking within a previously generated shape, but now I want to make the shapes appear wherever a click happens on the screen. I have tried to adjust the script, but I am just not figuring it out. The Gabors will not appear on screen, but the pause function is working (the script advances when I press ‘enter’).

Here is the main function of what used to work:

for i in range(3):
    rects = []
    for index, p in enumerate(all_pos):
        rect_index = visual.Rect(win, width=50, height=50, units='pix', lineWidth=5,
                                  fillColor=[200, 200, 200], lineColor=None, opacity=.2,
                                  autoDraw=False, pos=p)
        rects.append(rect_index)
    for r in rects:
        r.autoDraw = True
    win.flip()

    gabors_by_rect = {}  
    mouse_clicks = []
    paused = True
    prev_left = False
    prev_right = False

    while paused:
        if kb.getKeys('return'):
            paused = False
            continue

        left_pressed, middle_pressed, right_pressed = mouse.getPressed()

        for r in rects:
            if left_pressed and not prev_left and mouse.isPressedIn(r, buttons=[0]):
                if r not in gabors_by_rect:
                    gabor = visual.GratingStim(win, tex='sin', mask='gauss', texRes=256,
                                                units='pix', opacity=1, size=60, sf=.13,
                                                ori=0, contrast=1, name='gabor1', pos=r.pos)
                    gabor.autoDraw = True
                    gabors_by_rect[r] = gabor
                    mouse_clicks.append((r.pos[0], r.pos[1]))

            if right_pressed and not prev_right and mouse.isPressedIn(r, buttons=[2]):
                if r in gabors_by_rect:
                    gabors_by_rect[r].autoDraw = False
                    del gabors_by_rect[r]

        prev_left = left_pressed
        prev_right = right_pressed
        win.flip()

    for g in gabors_by_rect.values():
        g.autoDraw = False
    for r in rects:
        r.autoDraw = False
    win.flip()
    core.wait(1)

And here is what I have tried to adjust it to:

for i in range(3): 
    paused = True

    mouse_clicks = []
    gabors_drawn = {}
    prev_left = False
    prev_right = False

    while paused: 
        if kb.waitKeys(keyList = ['return']): 
            paused = False
            continue
        # initializing mouse 
        left_pressed, middle_pressed, right_pressed = mouse.getPressed()

        # the process begins 
        if left_pressed and not prev_left and mouse.getPressed(buttons = [0]): 
            click_pos = mouse.getPos()
            mouse_clicks.append(click_pos)
            gabor_to_draw = visual.GratingStim(win, tex = 'sin', mask = 'gauss', textRes = 256, 
                                                units = 'pix', opacity = 1, size = 60, sf = .13,
                                                ori = 0, contrast = 1, name = 'gabor1', pos = click_pos)
            gabor_to_draw.autoDraw = True
            gabors_drawn.append(gabor_to_draw)
            
            for g in gabors_drawn: 
                if right_pressed and prev_right and mouse.isPressedIn(g, buttons = [2]): 
                    gabors_drawn[g].autoDraw = False
        
        prev_left = left_pressed
        prev_right = right_pressed 
        win.flip()

    for g in gabors_drawn: 
        g.autoDraw = False
    win.flip()

I feel I could totally be missing something obvious. Would anyone know how to adjust that second block of code so the Gabors appear anywhere I click?

I will also attach these in a script to make it easier to access!

Thank you so much for your help!

Dear Lilih,

There’s a few things:

In gabor_to_draw, you have “textres” when I think you want “texres”.

Try changing kb.waitkeys() back to kb.getKeys(‘return’).

Also, based on your indents, you can only achieve your right click functionality (removing a gabor) if you are pressed right click the same frame as your left click.

See how that changes the functionality.

Issac