CopyrightC
CopyrightC

Reputation: 863

MOUSEBUTTONDOWN event not responding [pygame]

I'm not able to detect the event when Mouse button is pressed down.

Before asking this question I've tried:

import pygame, sys
pygame.init()
win = pygame.display.set_mode((1260, 960))
pygame.display.set_caption("PONG")

finish = False
fps = 60

clock = pygame.time.Clock()

while not finish:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.MOUSEBUTTONDOWN:
                print('MOUSE BUTTON PRESSED DOWN')
    clock.tick(fps)
    pygame.display.update()

What's wrong with the code?

Also, I'm not getting any errors in my terminal.

Upvotes: 4

Views: 311

Answers (2)

user14967983
user14967983

Reputation:

MOUSEBUTTONDOWN is not a keydown event

This should work

import pygame, sys
pygame.init()
win = pygame.display.set_mode((1260, 960))
pygame.display.set_caption("PONG")

finish = False
fps = 60

clock = pygame.time.Clock()

while not finish:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        
        if event.key == pygame.MOUSEBUTTONDOWN:
            print('MOUSE BUTTON PRESSED DOWN')
    clock.tick(fps)
    pygame.display.update()

Upvotes: 0

sloth
sloth

Reputation: 101072

Take a look at the documentation.

QUIT              none
ACTIVEEVENT       gain, state
KEYDOWN           key, mod, unicode, scancode
KEYUP             key, mod
MOUSEMOTION       pos, rel, buttons
MOUSEBUTTONUP     pos, button
MOUSEBUTTONDOWN   pos, button
JOYAXISMOTION     joy (deprecated), instance_id, axis, value
JOYBALLMOTION     joy (deprecated), instance_id, ball, rel
JOYHATMOTION      joy (deprecated), instance_id, hat, value
JOYBUTTONUP       joy (deprecated), instance_id, button
JOYBUTTONDOWN     joy (deprecated), instance_id, button
VIDEORESIZE       size, w, h
VIDEOEXPOSE       none
USEREVENT         code

You're looking for the MOUSEBUTTONDOWN event, not KEYDOWN:

for event in pygame.event.get():
    ...
    if event.type == pygame.MOUSEBUTTONDOWN:
        print('MOUSE BUTTON PRESSED DOWN')

Upvotes: 3

Related Questions