检查右键单击Python

时间:2018-03-26 09:47:31

标签: python python-3.x pygame

我正在尝试为右键单击添加事件侦听器,以便在按下右键时执行某些操作。我在谷歌上尝试了所有解决方案,但每个人都给我一个错误。我最终得到了

if pygame.event.type == pygame.MOUSEBUTTONUP:

没有打印,错误:

for event in pygame.event.get():
if event.type==pygame.MOUSEBUTTONDOWN: #other event types are pygame.QUIT, 
 pygame.MOUSEBUTTONUP, ...
    print("check1")
    if event.button==3: #1 is left, 2 is mouse wheel, and 3 is right
        print("check2")
  

AttributeError:'module'对象没有属性'type'

建议:

public static long getMaxMemory() {
    return Runtime.getRuntime().maxMemory();
}

public static long getUsedMemory() {
    return getMaxMemory() - getFreeMemory();
}

public static long getTotalMemory() {
    return Runtime.getRuntime().totalMemory();
}

public static long getFreeMemory() {
    return Runtime.getRuntime().freeMemory();
}

不起作用,它不会打印任何内容,也不会执行代码。

2 个答案:

答案 0 :(得分:0)

根据文档,pygame.event.type doesn't exist;你可以试试pygame.event.EventType.type吗?

答案 1 :(得分:0)

首先,pygame中的事件处理程序就像这样工作(pygame.event.get()为你提供了最近调用pygame.event.get()时发生的事件列表:

for event in pygame.event.get():
    #handle event

要检查鼠标按钮,您可以执行以下操作:

for event in pygame.event.get():
    if event.type==pygame.MOUSEBUTTONDOWN: #other event types are pygame.QUIT, pygame.MOUSEBUTTONUP, ...
        print("check1")
        if event.button==3: #1 is left, 2 is mouse wheel, and 3 is right
            print("check2")