从pygame的api,它有:
AudioSampleBuffer
但是没有办法区分右键,左键点击?
答案 0 :(得分:16)
if event.type == pygame.MOUSEBUTTONDOWN:
print event.button
event.button可以等于几个整数值:
1 - 左键单击
2 - 中间点击
3 - 右键单击
4 - 向上滚动
5 - 向下滚动
您也可以获取当前按钮状态,而不是事件:
pygame.mouse.get_pressed()
这会返回一个元组:
(leftclick,middleclick,rightclick)
每个都是一个布尔整数,代表按钮向上/向下。
答案 1 :(得分:5)
您可能需要仔细查看此tutorial以及this SO question对n.st的回答。
因此,向您展示如何区分右键和左键的代码如下所示:
#!/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 :(得分:0)
单击鼠标按钮,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()
返回代表所有鼠标按钮状态(True
或False
的布尔值列表。只要按住按钮,按钮的状态就为True
。当按下多个按钮时,列表中的多个项目为True
。列表中的第1、2和3rd元素分别表示鼠标左键,鼠标中键和鼠标右键。如果按下了特定按钮,则可以通过订阅进行评估:
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)