检测左,右鼠标点击我使用Python-2.7和pygame的建设扫雷艇。这几乎完成,只有一个重要的功能需要执行。当鼠标左键和右键被按下时,它会扫描周围的块并做一些事情。但我无法弄清楚如何同时检测两次点击。 在pygame的网站,有如何在同一时间在Pygame的
注意,上X11一些Xservers中使用鼠标中键模拟。当您同时点击按钮1和3时,可以发出2个按钮事件。
但它不为我工作了。
检测左,右鼠标点击我使用Python-2.7和pygame的建设扫雷艇。这几乎完成,只有一个重要的功能需要执行。当鼠标左键和右键被按下时,它会扫描周围的块并做一些事情。但我无法弄清楚如何同时检测两次点击。 在pygame的网站,有如何在同一时间在Pygame的
注意,上X11一些Xservers中使用鼠标中键模拟。当您同时点击按钮1和3时,可以发出2个按钮事件。
但它不为我工作了。
您可以为在文档中列出的场景代码(按钮2鼠标中被触发按钮1和3点按)。
mouse_pressed = pygame.mouse.get_pressed()
if (mouse_pressed[0] and mouse_pressed[2]) or mouse_pressed[1]:
print("left and right clicked")
记住,使用pygame.mouse.get_pressed()往往会引起非常快的,这意味着你的if语句会发生几次,然后才能松开鼠标左键,有任何代码。这是因为get_pressed()始终返回所有鼠标按钮的状态。如果您想跟踪正在按住的按钮,此方法很有用。
我将编码该溶液到pygame.event.get()。当一个事件发生这样只会触发的,因此只有一次:
left_mouse_down = False
right_mouse_down = False
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
left_mouse_down = True
if right_mouse_down:
print("perform action")
else:
print("action for single left click")
if event.button == 3:
right_mouse_down = True
if left_mouse_down:
print("perform action")
else:
print("action for single right click")
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
left_mouse_down = False
if event.button == 3:
right_mouse_down = False
UPDATE - 在两个键鼠标模拟第三鼠标点击
如果接受上面的代码工作,但只事实上,单左或右的行动将触发为好,但在组合前(左/右)动作。如果这适用于你的游戏设计,那就去做吧。如果这是不能接受的,那么下面的工作(虽然有点多深):
left_mouse_down = False
right_mouse_down = False
left_click_frame = 0 # keeps track of how long the left button has been down
right_click_frame = 0 # keeps track of how long the right button has been down
left_right_action_once = True # perform the combo action only once
left_action_once = True # perform the left action only once
right_action_once = True # perform the right action only once
max_frame = 2 # The frames to wait to see if the other mouse button is pressed (can be tinkered with)
performing_left_right_action = False # prevents the off chance one of the single button actions running on top of the combo action
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
mouse_pressed = pygame.mouse.get_pressed()
if mouse_pressed[0]: # left click
left_click_frame += 1
left_mouse_down = True
else:
left_action_once = True
left_mouse_down = False
left_click_frame = 0
if mouse_pressed[2]: # right click
right_click_frame += 1
right_mouse_down = True
else:
right_action_once = True
right_mouse_down = False
right_click_frame = 0
if not mouse_pressed[0] and not mouse_pressed[2]:
left_right_action_once = True
performing_left_right_action = False
if left_mouse_down and right_mouse_down and abs(left_click_frame - right_click_frame) <= max_frame:
if left_right_action_once:
print("performing right/left action")
left_right_action_once = False
performing_left_right_action = True
elif left_mouse_down and left_click_frame > max_frame and not performing_left_right_action and not right_mouse_down:
if left_action_once:
print("perform single left click action")
left_action_once = False
elif right_mouse_down and right_click_frame > max_frame and not performing_left_right_action and not left_mouse_down:
if right_action_once:
print("perform single right click action")
right_action_once = False
这个问题最难的是只有一个事件能在同一时间发生,所以你必须有一个办法知道左键单击被按下,不评估它,直到你确定右键点击也不会被按下。您可以通过跟踪帧,如果左做到这一点和一定数量的帧(max_frames)内发生的权利,那么你有一个右/左击。否则,你有左或右。
action_once变量很重要,因为由于pygame.mouse.get_pressed()每次通过游戏循环都检查每个鼠标按钮的状态,因此需要一个标志来防止您的操作在按钮被保持时运行多次向下(即使在短时间内点击)。游戏循环比点击鼠标更快。
因此,所有三种情况都包括在内:左单,右单,左右组合。换句话说,它模拟了在两个键鼠标“第三”鼠标点击....虽然该解决方案是非常笨拙
非常感谢。我想要检测左右单击而不是按住。看起来你的代码可以完美地检测到这种情况。但我也希望在只有左键或右键点击时执行其他操作。我觉得这些不能单独告诉两个点击情况吗? – orangedietc
你指的是类似击键的东西吗?或者只是单击左键? – The4thIceman
@orangedietc我编辑了我的代码来解释单个左键单击操作。阅读击键,你只需要做:elif event.type == pygame.KEYDOWN – The4thIceman
您应该同时测试pygame.mouse.get_pressed()[0]
(鼠标左键)和pygame.mouse.get_pressed()[2]
(鼠标左键)的值。
http://www.pygame.org/docs/ref/mouse.html#pygame.mouse.get_pressed
这个测试应该在pygame.event.get()
for循环,而不是同一级别包括在这个循环。您可以测试这个脚本,看看我的意思是:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800, 500), RESIZABLE)
running = True
while(running):
mousebuttons = pygame.mouse.get_pressed()
if mousebuttons[0] == 1 and mousebuttons[2] == 1:
print("Both left and rigth mouse buttons are pressed.")
for event in pygame.event.get():
if event.type == QUIT:
running = False
我使用'pygame.event.get()'来一次接受一个事件,因为如果没有,我不能让它工作,一个右键点击一个标志,另一个取消标志。我假设在这种情况下,'pygame.mouse.get_pressed()'中总是只有一个布尔值是真的? – orangedietc
你应该把'pygame.mouse.get_pressed()'部分放在'pygame.event.get()'for loop之外......我现在正在我的办公室工作,今天晚上给我点时间,我会告诉你一些简单的代码,通过编辑上面的答案来做你想做的事情;) – PurpleJo
这里我有更多的细节。该剧本在家完美运作。不要忘记验证这个答案,如果我让你的一天。请享用 ! – PurpleJo
如果你只是想你的代码按两个按钮只需设置right_mouse_down
和left_mouse_down
到False
后执行一次在你运行你的代码之后。
left_mouse_down = False
right_mouse_down = False
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
left_mouse_down = True
if right_mouse_down:
print("perform action")
right_mouse_down = False
left_mouse_down = True
if event.button == 3:
right_mouse_down = False
if left_mouse_down:
print("perform action")
right_mouse_down = False
left_mouse_down = False
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
left_mouse_down = False
if event.button == 3:
right_mouse_down = False
现在,这个代码将永远不会再运行,直到您删除并重新点击两个鼠标按钮。我建议使用pygame.mouse.get_pressed()
来检查任何时候按钮的状态,这样如果你只希望在第一次发生代码时可以设置一个标志。
done_the_thing = False
while not done: # Main game loop
buttons = pygame.mouse.get_pressed()
if buttons[0] and buttons[2] and not done_the_thing:
do_the_thing() # This is just your function
done_the_thing = True
if not buttons[0] and not buttons[2]:
done_the_thing = False
这将允许更多的灵活性,所以如果你想在确实发生每一帧或更改当前你在做什么更多的鼠标事件增加,你可以让他们的一起,干净。
您检测到右键单击,而左键仍然处于关闭状态,而另一种方式则是周围。您必须在每次向下/向上时保存按钮的状态才能执行此操作。 –
这是一个单击左右两个?或者你把他们放下? – The4thIceman