Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] Mouse Button Down Event?

edited March 2015 in OpenSesame

Does anyone have any experience of recording whether a mouse button is being held down? As in not just a click, but holding the button down like you would when dragging a window. I have experimented with the mouse.getpressed function but this doesn't seem to distinguish between click then release, and click then hold.

Alternatively, is there a way to record (on the android version) whether an area of the screen is being touched? I would like to make an event that only occurs when a participant lifts their finger off the screen.

Thanks!

Comments

  • edited 9:38AM

    Hi George,

    Me myself, I don't have much experience with mouse button down events or the android version. However, I'm quite sure that what you describe should be doable with OpenSesame. Maybe, you could have a look on this discussion? It seems to have a similar topic that your question has.

    Good luck and let us know if you need more help.

    Eduard

    Buy Me A Coffee

  • edited March 2015

    Hi George,

    The openexp.mouse.mouse.get_pressed does give information on which buttons are down!

    It returns a Boolean vector, basically a tuple with a 1/0 (down/up) value for all buttons. If you want participants to be able to drag something, you could very easily use the get_pressed output. Example:

    from openexp.canvas import canvas
    from openexp.mouse import mouse
    
    # create objects
    my_canvas = canvas(exp)
    my_mouse = mouse(exp)
    
    # stimulus specifics
    cpos = (100, 100)
    stimradius = 15
    
    while True:
        # get mouse position
        mpos, timestamp = my_mouse.get_pos()
        # get mouse button state
        # (e.g. (1,0,0) for a left button press)
        pressed = my_mouse.get_pressed()
        # if the left button is pressed, and the mouse is on the
        # moveable stimulus, update the position
        if pressed[0] == 1 and ((mpos[0]-cpos[0])**2 + (mpos[1]-cpos[1])**2)**0.5 < stimradius:
            cpos = mpos[:]
        # break if the right button is pressed
        elif pressed[2] == 1:
            break
        # clear the canvas
        my_canvas.clear()
        # draw stimulus
        my_canvas.circle(cpos[0], cpos[1], stimradius, color="#ff69b4", fill=True)
        # draw the mouse cursor
        my_canvas.fixdot(x=mpos[0], y=mpos[1], style="small-cross")
        # show canvas
        my_canvas.show()
    

    On your second question, of specifically picking up button releases: you could monitor the get_pressed output and check for transitions of 1 to 0. Alternatively, you could use PyGame directly (this is the underlying library for both the legacy and droid back-ends). To do this, you will need to monitor pygame.event.get() for a pygame.MOUSEBUTTONUP event (for more info, see the PyGame documentation).

    Good luck!

  • edited 9:38AM

    Thanks eduard and Edwin, that is really helpful. I have the task working now. My mistake on the mouse.get_pressed function!

Sign In or Register to comment.