如何在Pygame中同时检测鼠标左键和右键单击

橘子饮食等

我使用 python-2.7 和 pygame 来构建扫雷艇。差不多完成了,只剩下一个重要的功能需要实现。当同时按下鼠标左键和右键时,它会扫描周围的块并执行某些操作。但我无法弄清楚如何同时检测两次点击。在 pygame 的网站上,有

请注意,在 X11 上,某些 XServer 使用中键模拟。当您同时单击按钮 1 和 3 时,可以发出 2 按钮事件。

但这对我不起作用。

第四个冰人

您可以为文档中概述的场景编写代码(按下按钮 1 和按下按钮 3 时会触发鼠标中键 2)。

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

更新- 模拟第三次鼠标点击两键鼠标

上面的代码有效,但前提是您接受单个右或左动作也会触发的事实,但在组合(左/右)动作之前。如果这适用于您的游戏设计,那就试试吧。如果这是不可接受的,那么以下将起作用(尽管更深入一些):

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() 每次通过游戏循环都会检查每个鼠标按钮的状态,因此您需要一个标志来防止您的操作在按住按钮时多次运行(即使在点击的短暂持续时间)。游戏循环比鼠标点击快。

因此涵盖了所有三个场景:左单、右单、左右组合。换句话说,它模拟了在双键鼠标上的“第三次”鼠标点击......虽然解决方案相当笨重

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在pygame中区分左键单击,右键单击鼠标单击?

来自分类Dev

如何在Google Map API v3中区分鼠标单击事件中的鼠标左键和鼠标右键

来自分类Dev

如何检测右键单击+左键单击

来自分类Dev

如何在pygame中按下(鼠标左键)时检查mousemotion(和方向)?

来自分类Dev

如何在单击鼠标左键而不是单击鼠标右键时打开D3.js上下文菜单

来自分类Dev

如何在单击鼠标左键而不是单击鼠标右键时打开D3.js上下文菜单

来自分类Dev

通过同时左键和右键单击禁用中键模拟

来自分类Dev

如何在Angular中触发移位+鼠标左键单击

来自分类Dev

如何在swt.browser中启用左键单击拖动或右键单击

来自分类Dev

如何识别左键和右键单击以及0xFF

来自分类Dev

Python3-如何在pygame中检测鼠标是否在图像上单击

来自分类常见问题

如何检测pygame中嵌套按钮内的鼠标单击

来自分类Dev

想要鼠标左键和右键同时映射到两者进行选择

来自分类Dev

想要鼠标左键和右键同时映射到两者进行选择

来自分类Dev

按住鼠标左键并单击鼠标右键自动热键?

来自分类Dev

Listview右键单击拖动后单击鼠标左键删除项

来自分类Dev

左键和右键单击的不同功能

来自分类Dev

如何检测鼠标左键单击,而不是在UI按钮组件上单击时检测

来自分类Dev

如何在鼠标左键单击时禁用jqgrid的行选择

来自分类Dev

14.04-如何在ElanTech触摸板中同时启用“右键单击”和“多点触摸”功能?

来自分类Dev

鼠标左键/右键单击功能VB.NET

来自分类Dev

我想知道是否通过鼠标左键或右键单击QAction

来自分类Dev

鼠标左键/右键单击功能VB.NET

来自分类Dev

如何在JavaFX中检测到鼠标单击?

来自分类Dev

如何在GTK +中检测鼠标单击图像?

来自分类Dev

如何在文件夹/目录的右键单击鼠标菜单中添加“在终端中打开”?

来自分类Dev

如何正确检测鼠标右键?

来自分类Dev

根据android中的某些日期启用和禁用月份的日历左键和右键单击箭头

来自分类Dev

如何在TableView rowDelegate中单击鼠标右键显示上下文菜单

Related 相关文章

  1. 1

    如何在pygame中区分左键单击,右键单击鼠标单击?

  2. 2

    如何在Google Map API v3中区分鼠标单击事件中的鼠标左键和鼠标右键

  3. 3

    如何检测右键单击+左键单击

  4. 4

    如何在pygame中按下(鼠标左键)时检查mousemotion(和方向)?

  5. 5

    如何在单击鼠标左键而不是单击鼠标右键时打开D3.js上下文菜单

  6. 6

    如何在单击鼠标左键而不是单击鼠标右键时打开D3.js上下文菜单

  7. 7

    通过同时左键和右键单击禁用中键模拟

  8. 8

    如何在Angular中触发移位+鼠标左键单击

  9. 9

    如何在swt.browser中启用左键单击拖动或右键单击

  10. 10

    如何识别左键和右键单击以及0xFF

  11. 11

    Python3-如何在pygame中检测鼠标是否在图像上单击

  12. 12

    如何检测pygame中嵌套按钮内的鼠标单击

  13. 13

    想要鼠标左键和右键同时映射到两者进行选择

  14. 14

    想要鼠标左键和右键同时映射到两者进行选择

  15. 15

    按住鼠标左键并单击鼠标右键自动热键?

  16. 16

    Listview右键单击拖动后单击鼠标左键删除项

  17. 17

    左键和右键单击的不同功能

  18. 18

    如何检测鼠标左键单击,而不是在UI按钮组件上单击时检测

  19. 19

    如何在鼠标左键单击时禁用jqgrid的行选择

  20. 20

    14.04-如何在ElanTech触摸板中同时启用“右键单击”和“多点触摸”功能?

  21. 21

    鼠标左键/右键单击功能VB.NET

  22. 22

    我想知道是否通过鼠标左键或右键单击QAction

  23. 23

    鼠标左键/右键单击功能VB.NET

  24. 24

    如何在JavaFX中检测到鼠标单击?

  25. 25

    如何在GTK +中检测鼠标单击图像?

  26. 26

    如何在文件夹/目录的右键单击鼠标菜单中添加“在终端中打开”?

  27. 27

    如何正确检测鼠标右键?

  28. 28

    根据android中的某些日期启用和禁用月份的日历左键和右键单击箭头

  29. 29

    如何在TableView rowDelegate中单击鼠标右键显示上下文菜单

热门标签

归档