【问题标题】:How to distinguish left click , right click mouse clicks in pygame? [duplicate]如何区分pygame中的鼠标左键、右键单击? [复制]
【发布时间】:2016-03-21 04:01:32
【问题描述】:

来自pygame的api,它有:

event type.MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION

但是没有办法区分左右点击?

【问题讨论】:

标签: python event-handling pygame


【解决方案1】:

您可能想仔细看看这个tutorial,以及n.st 对this SO question 的回答。

因此,向您展示如何区分右键和左键的代码如下所示:

#!/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()

【讨论】:

    【解决方案2】:

    点击事件


    if event.type == pygame.MOUSEBUTTONDOWN:
        print(event.button)
    

    event.button 可以等于多个整数值:

    1 - left click
    2 - middle click
    3 - right click
    4 - scroll up
    5 - scroll down
    

    获取鼠标状态


    您也可以获取当前按钮状态,而不是等待事件:

    state = pygame.mouse.get_pressed()
    

    这会返回一个元组,格式为:(leftclick, middleclick, rightclick)

    每个值都是一个布尔整数,表示该按钮是否被按下。

    【讨论】:

      【解决方案3】:

      单击鼠标按钮时会发生一次MOUSEBUTTONDOWN 事件,松开鼠标按钮时会发生一次MOUSEBUTTONUP 事件。 pygame.event.Event() 对象有两个属性,提供有关鼠标事件的信息。每个鼠标按钮都关联一个值。例如,鼠标左键、鼠标中键、鼠标右键、鼠标滚轮向上和鼠标滚轮向下的属性值为 1、2、3、4、5。当按下多个键时,会发生多个鼠标按钮事件。进一步的解释可以在模块pygame.event的文档中找到:

      run = True
      while run:
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  run = False
      
              if event.type == pygame.MOUSEBUTTONDOWN:
                  if event.button == 1:
                      print("left mouse button")
                  elif event.button == 2:
                      print("middle mouse button")
                  elif event.button == 3:
                      print("right mouse button")
                  elif event.button == 4:
                      print("mouse wheel up")
                  elif event.button == 5:
                      print("mouse wheel down")
      

      也可以使用pygame.mouse.get_pressed()pygame.mouse.get_pressed() 返回一个布尔值列表,代表所有鼠标按钮的状态(TrueFalse)。只要按下按钮,按钮的状态就是True。当按下多个按钮时,列表中的多个项目为True。列表中的第 1、2、3 个元素代表鼠标左键、中键和右键。如果按下特定按钮,则可以通过订阅来评估:

      run = True
      while run:
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  run = False
      
          mouse_buttons = pygame.mouse.get_pressed()
      
          button_msg = ""
          if mouse_buttons[0]:
              button_msg += "left mouse button  "
          if mouse_buttons[1]:
              button_msg += "middel mouse button  "
          if mouse_buttons[2]:
              button_msg += "right mouse button  "
      
          if button_msg == "":
              print("no button pressed")
          else:
              print(button_msg)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 2010-10-18
        • 1970-01-01
        相关资源
        最近更新 更多