From api of pygame, it has:
event type.MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION
But there is no way to distinguish between right, left clicks?
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()
来源:https://stackoverflow.com/questions/34287938/how-to-distinguish-left-click-right-click-mouse-clicks-in-pygame