How to determine if a user has been double clicked in pygame?

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? Is there also a way to check if the user has moved the scroll wheel forward or backward?

+6
source share
5 answers

I have never used pygame- but:

  • Double-click detection: when guessing, instead of processing each click at once, apply a delay of 50 ms and see if another click event occurred at that time. The user will probably not notice a delay of 50 ms.

  • scrollwheel /: . . , - , , ​​, . scrollwheel , - SCROLL_UP LEFT.

    , , SCROLL_UP.

+2

, clock.tick , . 0,5 , 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()
+10

, , userevent pygame 1, . , . , userevent, , . . : Pygame

, 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"
+2

, . , MOUSEBUTTONDOWN.

pygame.MOUSEBUTTONDOWN . ​​ 4, , 5, .

+1

- pygame.time.Clock() MOUSEBUTTONDOWN.

:

dbclock = pygame.time.Clock()

:

if event.type == pygame.MOUSEBUTTONDOWN:
    if dbclock.tick() < DOUBLECLICKTIME:
        print("double click detected!")

DOUBLECLICKTIME - ( ) , . . , : DOUBLECLICKTIME = 500.

pygame pygame.time.Clock() pygame.time.Clock(). dbclock ( , dbclock.tick() ) .


, .
MOUSEBUTTONDOWN MOUSEBUTTONUP ( ). event.button, 4 , 5 .

0

All Articles