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

心不动则不痛 提交于 2019-11-29 13:51:15
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


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

pygame.mouse.get_pressed()

This returns a tuple:

(leftclick, middleclick, rightclick)

Each one is a boolean integer representing button up/down.

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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!