How to distinguish left click , right click mouse clicks in pygame? [duplicate]
Asked Answered
M

3

22

From api of pygame, it has:

event type.MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION

But there is no way to distinguish between right, left clicks?

Misinterpret answered 15/12, 2015 at 11:23 Comment(1)
pygame.org/docs/ref/event.htmlDeppy
D
50

Click events


if event.type == pygame.MOUSEBUTTONDOWN:
    print(event.button)

event.button can equal several integer values:

1 - left click
2 - middle click
3 - right click
4 - scroll up
5 - scroll down

Fetching mouse state


Instead of waiting for an event, you can get the current button state as well:

state = pygame.mouse.get_pressed()

This returns a tuple in the form: (leftclick, middleclick, rightclick)

Each value is a boolean integer representing whether that button is pressed.

Drivel answered 16/12, 2015 at 1:45 Comment(0)
D
10

You may want to take a closer look at this tutorial, as well as at the n.st's answer to this SO question.

So the code that shows you how to distinguish between the right and left click goes like this:

#!/usr/bin/env python
import pygame

LEFT = 1
RIGHT = 3

running = 1
screen = pygame.display.set_mode((320, 200))

while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = 0
    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
        print "You pressed the left mouse button at (%d, %d)" % event.pos
    elif event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:
        print "You released the left mouse button at (%d, %d)" % event.pos
    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == RIGHT:
        print "You pressed the right mouse button at (%d, %d)" % event.pos
    elif event.type == pygame.MOUSEBUTTONUP and event.button == RIGHT:
        print "You released the right mouse button at (%d, %d)" % event.pos

    screen.fill((0, 0, 0))
    pygame.display.flip()
Deify answered 15/12, 2015 at 11:47 Comment(0)
H
2

The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released. The pygame.event.Event() object has two attributes that provide information about the mouse event. Each mouse button is associated a value. For instance the value of the attributes is 1, 2, 3, 4, 5 for the left mouse button, middle mouse button, right mouse button, mouse wheel up, mouse wheel down, respectively. When multiple keys are pressed, multiple mouse button events occur. Further explanations can be found in the documentation of the module pygame.event:

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                print("left mouse button")
            elif event.button == 2:
                print("middle mouse button")
            elif event.button == 3:
                print("right mouse button")
            elif event.button == 4:
                print("mouse wheel up")
            elif event.button == 5:
                print("mouse wheel down")

Alternatively pygame.mouse.get_pressed() can be used. pygame.mouse.get_pressed() returns a list of Boolean values ​​that represent the state (True or False) of all mouse buttons. The state of a button is True as long as a button is held down. When multiple buttons are pressed, multiple items in the list are True. The 1st, 2nd and 3rd elements in the list represent the left, middle and right mouse buttons. If a specific button is pressed, this can be evaluated by subscription:

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    mouse_buttons = pygame.mouse.get_pressed()

    button_msg = ""
    if mouse_buttons[0]:
        button_msg += "left mouse button  "
    if mouse_buttons[1]:
        button_msg += "middle mouse button  "
    if mouse_buttons[2]:
        button_msg += "right mouse button  "

    if button_msg == "":
        print("no button pressed")
    else:
        print(button_msg)
Hagiography answered 31/10, 2020 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.