如何区分pygame中的鼠标左键单击、右键单击?

How to distinguish left click , right click mouse clicks in pygame?

从 api 到 pygame,它有:

event type.MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION

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

你可能想仔细看看这个tutorial, as well as at the n.st's answer to 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()

点击事件


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()

这个 returns 形式的元组:(leftclick, middleclick, rightclick)

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

单击鼠标时MOUSEBUTTONDOWN事件发生一次,松开鼠标时MOUSEBUTTONUP事件发生一次。 pygame.event.Event() object has two attributes that provide information about the mouse event. Each mouse button is associated a value. For instance the value of the attributes is 1, 2, 3, 4, 5 for the left mouse button, middle mouse button, right mouse button, mouse wheel up, mouse wheel down, respectively. When multiple keys are pressed, multiple mouse button events occur. Further explanations can be found in the documentation of the module 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() returns 代表所有鼠标按钮状态(TrueFalse)的布尔值列表。只要按下一个按钮,按钮的状态就是True。当按下多个按钮时,列表中的多个项目是True。列表中的第一个、第二个和第三个元素代表鼠标左键、中键和右键。如果按下特定按钮,则可以通过订阅进行评估:

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 += "middle mouse button  "
    if mouse_buttons[2]:
        button_msg += "right mouse button  "

    if button_msg == "":
        print("no button pressed")
    else:
        print(button_msg)