How can I detect if the user has double-clicked in pygame?

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.

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()

I've never used pygame - but:

  • 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.

  • 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.

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


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

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

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"

Tags:

Python

Pygame