How to make the pygame mouse continue to return events using the pygame event loop

Viewed 46

I would like to know how to make it so that when you hold down the pygame mouse, it continues getting events in the event loop.
I do not want to use pygame.mouse.get_pressed() because I want to also have a hold-down time to where it doesn't do anything.
Kind of like if you hold down a key in a text box in your browser: it types one key, then waits for about half a second, then starts typing a lot of keys really fast.
Also, I think it involves something before the main loop when the window is created or something, but I'm not sure.
I know there's a way to do this in pygame because I saw a Stack Overflow answer to it before, but after a while of searching, I cannot find it. If you have seen it or can find it, that is also appreciated too. This is my first time asking a question, so if I did anything wrong, please tell me.
Here is the code:

import pygame

WIDTH = 1000
HEIGHT = 800
WIN = pygame.display.set_mode()

def main():
    run = True 
    clock = pygame.time.Clock()
    
    while run:
        clock.tick(60)
      
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                print("the mouse was pressed")
        
        WIN.fill((0, 0, 0))

        pygame.display.update()

1 Answers

I would like to know how to make it so that when you hold down the pygame mouse, it continues getting events in the event loop.

Yo can't. However you can use pygame.mouse.get_pressed():

import pygame

WIDTH = 1000
HEIGHT = 800
WIN = pygame.display.set_mode()

def main():
    run = True 
    clock = pygame.time.Clock()
    
    while run:
        clock.tick(60)
      
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
  
        buttons = pygame.mouse.get_pressed() 
        if any(buttons):
            print("the mouse was pressed")

        WIN.fill((0, 0, 0))
        pygame.display.update()

The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released.
pygame.mouse.get_pressed() returns a list of Boolean values ​​that represent the current state (True or False) of all mouse buttons. The state of a button is True as long as a button is held down.

Related