Home > Software engineering >  Detect Both Left and Right Mouse Clicks
Detect Both Left and Right Mouse Clicks

Time:12-04

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.

CodePudding user response:

You have to combine the pygame.mouse.get_pressed() and the MOUSEBUTTONDOWN event. The buttons are never clicked at the same moment. The events occur one at a time, but pygame.mouse.get_pressed() indicates when a key is held down. Detect when a button is clicked with MOUSEBUTTONDOWN and use pygame.mouse.get_pressed() to check if the other button is held:

run = True
while run:
      event = pygame.event.poll()
      buttons = pygame.mouse.get_pressed()
      if evet.type == pygame.QUIT:
          run = False 
      
      elif event.type == pygame.MOUSEBUTTONDOWN:
          
          # left clicked and right hold down
          if event.button == 1 and mouse_buttons[2]: 
          
              # [...]

          # right clicked and left hold down
          elif event.button == 3 and mouse_buttons[1]: 

              # [...]
      

CodePudding user response:

I've re-factored your event handling:

import pygame, sys

screen = pygame.display.set_mode((320, 200))
left = False
right = False

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False 
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:  # LMB
                print('left press')
                left = True
            elif event.button == 3:  # RMB
                print('right press')
                right = True
        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:
                print('left release')
                left = False
            elif event.button == 3:
                print('right release')
                right = False
                
    screen.fill((0, 0, 0))
    pygame.display.flip()
pygame.quit()
sys.exit()    

The test cases you've described work as expected, e.g. left-click, right-click, right-release, left-release displays:

left press
right press
right release
left release
  • Related