Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

From api of pygame, it has:

event type.MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION

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

like image 865
ERJAN Avatar asked Dec 15 '15 11:12

ERJAN


People also ask

How does pygame know double click?

type == pygame. MOUSEBUTTONDOWN: if dbclock. tick() < DOUBLECLICKTIME: print("double click detected!")

How do I know if my left mouse button is pressed pygame?

The mouse module has functions that lets us check and set the position of the mouse as well as check the buttons pressed. The function pygame. mouse. get_pressed() returns a tuple tuple representing if the mouse buttons (left, mouse-wheel, right) is being pressed or not.

How do I know if my mouse is clicking pygame?

The current position of the mouse can be determined via pygame. mouse. get_pos() . The return value is a tuple that represents the x and y coordinates of the mouse cursor.


3 Answers

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.

like image 173
Spooky Avatar answered Oct 09 '22 20:10

Spooky


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()
like image 34
vrs Avatar answered Oct 09 '22 22:10

vrs


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)
like image 1
Rabbid76 Avatar answered Oct 09 '22 22:10

Rabbid76