Search code examples
pythonpyglet

Pyglet incorrectly interpreting all mouse clicks as left clicks


I am trying to use pyglet on ubuntu 20.04, and the code is working except I have specified certain behavior on mouse drag with the left button held, (intending different behavior on when middle or right buttons are held) but mouse.LEFT is true even if it isn't a left mouse button, I have inserted a snippet below.

@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):

    if mouse.LEFT:
        chart.x_offset += float(dx)
        chart.y_offset += float(dy)
    elif mouse.RIGHT:
        chart.y_scale+=dy

It feels like it might be a bug/issue with interpreting mouse signals on Ubuntu, but I have no idea really, I am new to pyglet.

Thanks for reading


Solution

  • mouse.LEFT and mouse.RIGHT are constants. You have to evaluate if a specific bit is set in the buttons argument:

    @window.event
    def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
    
        if buttons & mouse.LEFT:
            chart.x_offset += float(dx)
            chart.y_offset += float(dy)
    
        if buttons & mouse.RIGHT:
            chart.y_scale += dy
    

    See further pyglet - Working with the mouse.