0

Is there any function to check if the mouse button is pressed or not . MOUSEBUTTONDOWN doesn't work

if event.type == pygame.MOUSEBUTTONDOWN:
    mouse_pos = event.pos

I have to release the button then press it again , then the code works . If I kept pressing the button it doesn't work

I want that if I kept pressing the button , the code still works

1 Answers1

0

You can use pygame.mouse.get_pressed() to get list of button states.

Another way of achieving that is by polling every event and setting your value to True on MOUSEBUTTONDOWN events and to False on MOUSEBUTTONUP events.

hkc
  • 61
  • 1
  • 4
  • "*Just don't forget about setting it to False in MOUSEBUTTONUP event.*"? What does that mean? You don't need to do that – D_00 May 21 '21 at 08:34
  • Well, if they don't set it back to False in MOUSEBUTTONUP event, it will be stuck at True after first mouse press – hkc Jun 08 '21 at 12:45
  • So you are saying that you are putting `pygame.mouse.get_pressed()` in the `if event.type == MOUSEBUTTONDOWN`? That makes no sense, just put `pygame.mouse.get_pressed()` in the main loop. You don't need to put this instruction in the `for event in pygame.event.get()` loop. – D_00 Jun 08 '21 at 13:37
  • I mean, that you can use `pygame.mouse.get_pressed` OR check for every event and if it's `MOUSEBUTTONDOWN` event, set your value to True and if it is `MOUSEBUTTONUP`, set it to False. That's two solutions to one problem. Maybe I should describe it better – hkc Jun 14 '21 at 22:18
  • Ok, I understand now. But the easiest solution, the simplest, the shortest, and the one which uses the least amount of time is the use of `pygame.mouse.get_pressed()` here. – D_00 Jun 15 '21 at 06:19