如何检测用户是否在pygame中双击?

时间:2021-10-10 06:40:42

I know I can check if there was a left click

我知道我可以检查是否有左键单击

event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT

but how can I check if they double clicked? Also is there any way to check if the user moved the scroll wheel forward or backwards?

但我如何检查他们是否双击?还有什么方法可以检查用户是向前还是向后移动滚轮?

4 个解决方案

#1


2  

I've never used pygame - but:

我从来没用过pygame - 但是:

  • Detecting double clicks: at a guess, instead of processing each click immediately, apply a 50ms delay and see if you get another click event in that time. The user probably won't notice the 50ms delay.

    检测双击:猜测,而不是立即处理每次点击,应用50毫秒的延迟,看看你是否在那段时间内再次点击事件。用户可能不会注意到50ms的延迟。

  • Distinguishing between scrollwheel up/down: see the comments on this documentation page. Apparently there are five buttons defined - left, middle, right, scrollwheel-up and scrollwheel-down. That is, you can capture scrollwheel events the same way you're capturing left clicks - you just need to look for SCROLL_UP or similar instead of LEFT.

    滚动向上/向下区分:请参阅本文档页面上的注释。显然有五个按钮定义 - 左,中,右,滚轮向上和向下滚轮。也就是说,您可以像捕获左键单击一样捕获滚轮事件 - 您只需要查找SCROLL_UP或类似而不是LEFT。

    Look up the documentation to find out exactly what SCROLL_UP is called.

    查找文档以确切了解SCROLL_UP的调用内容。

#2


8  

I'd just use the delta time value that clock.tick returns to increase a timer. In this example you have 0.5 seconds to double click otherwise the timer is reset.

我只是使用clock.tick返回的delta时间值来增加一个计时器。在此示例中,您需要0.5秒才能双击,否则计时器将重置。

import sys
import pygame as pg


pg.init()

screen = pg.display.set_mode((640, 480))
BLACK = pg.Color('black')
FONT = pg.font.Font(None, 32)


def game():
    clock = pg.time.Clock()
    timer = 0
    dt = 0
    running = True

    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            if event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if timer == 0:  # First mouse click.
                        timer = 0.001  # Start the timer.
                    # Click again before 0.5 seconds to double click.
                    elif timer < 0.5:
                        print('double click')
                        timer = 0

        # Increase timer after mouse was pressed the first time.
        if timer != 0:
            timer += dt
            # Reset after 0.5 seconds.
            if timer >= 0.5:
                print('too late')
                timer = 0

        screen.fill(BLACK)
        txt = FONT.render(str(round(timer, 2)), True, (180, 190, 40))
        screen.blit(txt, (40, 40))
        pg.display.flip()
        # dt == time in seconds since last tick.
        # / 1000 to convert milliseconds to seconds.
        dt = clock.tick(30) / 1000


if __name__ == '__main__':
    game()
    pg.quit()
    sys.exit()

#3


2  

Set a timer when the mouse is pressed the first time to place a userevent on the pygame event queue, and set a variable to 1 to indicate a click. When the second click occurs, check the variable and set the timer event object to off. Check if the userevent comes up on the queue as this means the timer has timed out. see this beautiful answer for more information: Move an object every few seconds in Pygame

第一次按下鼠标时设置计时器以在pygame事件队列上放置userevent,并将变量设置为1以指示单击。发生第二次单击时,检查变量并将timer事件对象设置为off。检查队列中是否存在userevent,因为这意味着计时器已超时。看到这个美丽的答案获取更多信息:在Pygame中每隔几秒移动一个对象

Here is the code, replace the double_click() call with your own function call:

这是代码,用你自己的函数调用替换double_click()调用:

def run():

    global clock, double_click_event, timer
    double_click_event = pygame.USEREVENT + 1
    timer = 0

    while True:
        clock.tick(60)
        check_events()
        frame.update()
        screen.blit(frame, (0,0))
        pygame.display.flip()   


def check_events():
    global dispatcher, double_click_event, timer

    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if timer == 0:
                pygame.time.set_timer(double_click_event, 500)
                timerset = True
            else:
                if timer == 1:
                    pygame.time.set_timer(double_click_event, 0)
                    double_click()
                    timerset =False

            if timerset:
                timer = 1
                return
            else: 
                timer = 0
                return

        elif event.type == double_click_event:
            # timer timed out
            pygame.time.set_timer(double_click_event, 0)
            timer = 0
            print "evt = dble click"

#4


1  

There doesn't appear to be a native double-click event. I'll guess you'd need to check the time between consecutive MOUSEBUTTONDOWN events.

似乎没有本机双击事件。我猜你需要检查连续的MOUSEBUTTONDOWN事件之间的时间。

The mouse wheel will generate pygame.MOUSEBUTTONDOWN events when rolled. The button will be set to 4 when the wheel is rolled up, and to button 5 when the wheel is rolled down

滚动时,鼠标滚轮将生成pygame.MOUSEBUTTONDOWN事件。当车轮卷起时按钮将设置为4,当车轮向下滚动时按钮5将设置为按钮5

#1


2  

I've never used pygame - but:

我从来没用过pygame - 但是:

  • Detecting double clicks: at a guess, instead of processing each click immediately, apply a 50ms delay and see if you get another click event in that time. The user probably won't notice the 50ms delay.

    检测双击:猜测,而不是立即处理每次点击,应用50毫秒的延迟,看看你是否在那段时间内再次点击事件。用户可能不会注意到50ms的延迟。

  • Distinguishing between scrollwheel up/down: see the comments on this documentation page. Apparently there are five buttons defined - left, middle, right, scrollwheel-up and scrollwheel-down. That is, you can capture scrollwheel events the same way you're capturing left clicks - you just need to look for SCROLL_UP or similar instead of LEFT.

    滚动向上/向下区分:请参阅本文档页面上的注释。显然有五个按钮定义 - 左,中,右,滚轮向上和向下滚轮。也就是说,您可以像捕获左键单击一样捕获滚轮事件 - 您只需要查找SCROLL_UP或类似而不是LEFT。

    Look up the documentation to find out exactly what SCROLL_UP is called.

    查找文档以确切了解SCROLL_UP的调用内容。

#2


8  

I'd just use the delta time value that clock.tick returns to increase a timer. In this example you have 0.5 seconds to double click otherwise the timer is reset.

我只是使用clock.tick返回的delta时间值来增加一个计时器。在此示例中,您需要0.5秒才能双击,否则计时器将重置。

import sys
import pygame as pg


pg.init()

screen = pg.display.set_mode((640, 480))
BLACK = pg.Color('black')
FONT = pg.font.Font(None, 32)


def game():
    clock = pg.time.Clock()
    timer = 0
    dt = 0
    running = True

    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            if event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if timer == 0:  # First mouse click.
                        timer = 0.001  # Start the timer.
                    # Click again before 0.5 seconds to double click.
                    elif timer < 0.5:
                        print('double click')
                        timer = 0

        # Increase timer after mouse was pressed the first time.
        if timer != 0:
            timer += dt
            # Reset after 0.5 seconds.
            if timer >= 0.5:
                print('too late')
                timer = 0

        screen.fill(BLACK)
        txt = FONT.render(str(round(timer, 2)), True, (180, 190, 40))
        screen.blit(txt, (40, 40))
        pg.display.flip()
        # dt == time in seconds since last tick.
        # / 1000 to convert milliseconds to seconds.
        dt = clock.tick(30) / 1000


if __name__ == '__main__':
    game()
    pg.quit()
    sys.exit()

#3


2  

Set a timer when the mouse is pressed the first time to place a userevent on the pygame event queue, and set a variable to 1 to indicate a click. When the second click occurs, check the variable and set the timer event object to off. Check if the userevent comes up on the queue as this means the timer has timed out. see this beautiful answer for more information: Move an object every few seconds in Pygame

第一次按下鼠标时设置计时器以在pygame事件队列上放置userevent,并将变量设置为1以指示单击。发生第二次单击时,检查变量并将timer事件对象设置为off。检查队列中是否存在userevent,因为这意味着计时器已超时。看到这个美丽的答案获取更多信息:在Pygame中每隔几秒移动一个对象

Here is the code, replace the double_click() call with your own function call:

这是代码,用你自己的函数调用替换double_click()调用:

def run():

    global clock, double_click_event, timer
    double_click_event = pygame.USEREVENT + 1
    timer = 0

    while True:
        clock.tick(60)
        check_events()
        frame.update()
        screen.blit(frame, (0,0))
        pygame.display.flip()   


def check_events():
    global dispatcher, double_click_event, timer

    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if timer == 0:
                pygame.time.set_timer(double_click_event, 500)
                timerset = True
            else:
                if timer == 1:
                    pygame.time.set_timer(double_click_event, 0)
                    double_click()
                    timerset =False

            if timerset:
                timer = 1
                return
            else: 
                timer = 0
                return

        elif event.type == double_click_event:
            # timer timed out
            pygame.time.set_timer(double_click_event, 0)
            timer = 0
            print "evt = dble click"

#4


1  

There doesn't appear to be a native double-click event. I'll guess you'd need to check the time between consecutive MOUSEBUTTONDOWN events.

似乎没有本机双击事件。我猜你需要检查连续的MOUSEBUTTONDOWN事件之间的时间。

The mouse wheel will generate pygame.MOUSEBUTTONDOWN events when rolled. The button will be set to 4 when the wheel is rolled up, and to button 5 when the wheel is rolled down

滚动时,鼠标滚轮将生成pygame.MOUSEBUTTONDOWN事件。当车轮卷起时按钮将设置为4,当车轮向下滚动时按钮5将设置为按钮5