How to distinguish left click, right click in pygame?

From api pygame it has:

event type.MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION 

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

+5
source share
2 answers
 if event.type == pygame.MOUSEBUTTONDOWN: print event.button 

event.button can equal several integer values:

1 - left click

2 - medium click

3 - right click

4 - scroll up

5 - scroll down


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

 pygame.mouse.get_pressed() 

This returns a tuple:

(leftclick, middleclick, rightclick)

Each of them is a logical integer representing an up / down button.

+8
source

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

So the code showing how to distinguish between right and left click looks 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() 
+4
source

Source: https://habr.com/ru/post/1238253/


All Articles