Single mouse click within While loop (PsychoPy)
Question: I am using PsychoPy and I would like to print the position of my mouse when it
has been clicked.

The actual printing of the position needs to be placed inside a while loop.
Using the code below, when I click I get more than one output lines, which
print the same positions. I would like to have only one output printing for
each click.

This is the code I am using:

    
    
    #!/usr/bin/env python2
    from psychopy import visual, core, event
    from pyglet.gl import *
    
    width = 600
    height = 600
    myWin = visual.Window([width,height], color='white',units='pix',monitor='testMonitor')
    
    #This will set the windows units (pixels) to GL units
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0, width, 0, height, -1, 1)
    
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    
    glEnable(GL_BLEND)
    glBlendFunc(GL_ZERO, GL_SRC_COLOR)
    
    myMouse = event.Mouse()  #  will use myWin by default
    
    
    while True:
        #Triangle left
        glColor3f(1.0, 0.0, 0.0)
        glBegin(GL_TRIANGLES)
        glVertex3f(150, 550, 1)
        glVertex3f(50, 350, 1)
        glVertex3f(250, 350, -1)
        glEnd()
    
        if myMouse.getPressed()[0]:
            myMouse.clickReset()
            print myMouse.getPos()
    
        myWin.flip()
    
    core.quit()
    

Is there something wrong I am doing? Should the 'frame rate' of the while loop
be changed?


Answer: ~~I've not used the module, but it seems like mouse events are thrown for
mouse _raises_ as well.~~

You'll need to

  * Store the mouse state for next time

  * Each iteration, test whether the mouse state for button 0 has gone up → down

The mouse state is returned by `myMouse.getPressed`.

So something like:

    
    
    oldMouseIsDown = False
    
    while True:
        ...
    
        mouseIsDown = myMouse.getPressed()[0]
        myMouse.clickReset()
    
        if mouseIsDown and not oldMouseIsDown:
            print myMouse.getPos()
    
        oldMouseIsDown = mouseIsDown
    

