Python 如何在pygame中区分左键单击,右键单击鼠标单击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34287938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to distinguish left click , right click mouse clicks in pygame?
提问by ERJAN
From api of pygame, it has:
从pygame的api,它有:
event type.MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION
But there is no way to distinguish between right, left clicks?
但是没有办法区分左右点击吗?
采纳答案by Neon Wizard
if event.type == pygame.MOUSEBUTTONDOWN:
print event.button
event.button can equal several integer values:
event.button 可以等于几个整数值:
1 - left click
1 - 左键单击
2 - middle click
2 - 中键
3 - right click
3 - 右键单击
4 - scroll up
4 - 向上滚动
5 - scroll down
5 - 向下滚动
Instead of an event, you can get the current button state as well:
除了事件之外,您还可以获取当前按钮状态:
pygame.mouse.get_pressed()
This returns a tuple:
这将返回一个元组:
(leftclick, middleclick, rightclick)
(左键单击、中键单击、右键单击)
Each one is a boolean integer representing button up/down.
每个都是一个布尔整数,代表按钮向上/向下。
回答by vrs
You may want to take a closer look at this tutorial, as well as at the n.st's answer to this SO question.
So the code that shows you how to distinguish between the right and left click goes like this:
因此,向您展示如何区分右键单击和左键单击的代码如下所示:
#!/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()