I'm writing a game in replit with pygame, and I want pygame to detect left and right clicks at the same time. I've looked at questions such as this and this, but they don't solve my problem. Using buttons = pygame.mouse.get_pressed()
, I can only detect one click at a time. It functions properly when I left click or right click, but I cannot get both left and right click. If I hold left, I will have to double click right in order for it to detect it. I'd like it to be detected the first time.
Minimum Working Example:
import pygame, sys
screen = pygame.display.set_mode((320, 200))
left = False
right = False
while True:
event = pygame.event.poll()
buttons = pygame.mouse.get_pressed()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and buttons[0] and not left:
print('left press')
left = True
elif event.type == pygame.MOUSEBUTTONUP and not buttons[0] and left:
print('left release')
left = False
elif event.type == pygame.MOUSEBUTTONDOWN and buttons[2] and not right:
print('right press')
right = True
elif event.type == pygame.MOUSEBUTTONUP and not buttons[2] and right:
print('right release')
right = False
screen.fill((0, 0, 0))
pygame.display.flip()
Typical Printed Output:
*left click and then release*
left press
left release
*right click and then release*
right press -- buttons = (True, False, False), left = False, right = False
right release -- buttons = (False, False, False), left = True, right = False
*left click, right click, release both*
left press -- buttons = (True, False, False), left = False, right = False
left release -- buttons = (False, False, False), left = True, right = False
*right click, left click, release both*
right press -- buttons = (False, False, True), left = False, right = False
right release -- buttons = (False, False, False), left = False, right = True
*left click, right double click, release right, release left*
left press -- buttons = (True, False, False), left = False, right = False
right press -- buttons = (True, False, True), left = True, right = False
right release -- buttons = (True, False, False), left = True, right = True
left release -- buttons = (False, False, False), left = True, right = False
*left click, right double click, release left, release right*
left press -- buttons = (True, False, False), left = False, right = False
right press -- buttons = (True, False, True), left = True, right = False
left release -- buttons = (False, False, True), left = True, right = True
right release -- buttons = (False, False, False), left = False, right = True
Same situation for the mirrored case of right click and left double click.