[Python] Detecting double-clicks in Pygame [duplicate]

Discussão em 'Python' iniciado por Stack, Maio 10, 2025.

  1. Stack

    Stack Membro Participativo

    Im trying to detect the double-clicks to a button or mouse click on Pygame. I tried millis-like method but it doesnt work. How can i both detect normal press-and-hold's and double clicks of a button?

    while running:
    current_time = pygame.time.get_ticks()
    for event in pygame.event.get():
    if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
    running = False
    elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
    current_time = pygame.time.get_ticks()
    if current_time - last_space_press_time < double_click_threshold:
    os.system("pkill mpg123")
    last_space_press_time = current_time
    if not recording:
    start_recording()
    elif event.type == pygame.KEYUP and event.key == pygame.K_SPACE and recording:
    stop_recording()
    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
    current_time = pygame.time.get_ticks()
    if current_time - last_mouse_press_time < double_click_threshold:
    os.system("pkill mpg123")
    last_mouse_press_time = current_time
    if is_in_exit_area(event.pos):
    running = False
    elif not recording and not mouse_recording:
    mouse_recording = True
    start_recording()
    elif event.type == pygame.MOUSEBUTTONUP and event.button == 1 and mouse_recording:
    mouse_recording = False
    if recording:
    stop_recording()


    No errors tho, its just triggering start_recording function and even i double-click, it doesn't run "pkill mpg123". Thanks :)

    Continue reading...

Compartilhe esta Página