Mouse wheel event in Pygame

Here in this tutorial, we will see how to detect mouse wheel movement in Pygame Python.

Detecting mouse wheel event

import pygame

pygame.init()

screen = pygame.display.set_mode((500, 450))

while True:
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            pygame.quit()
            exit()

        if i.type == pygame.MOUSEWHEEL:
            print("Mouse wheel movement detected")

    pygame.display.update()

Detecting the Mouse wheel event in Pygame is simple as Pygame provides an inbuilt function to do so.

In the source code written above once Pygame has been imported and initialized we have used the pygame.event.get function to exit the game as well as to detect the mouse wheel movement;

pygame.MOUSEWHEEL function helps us detect if there is any movement in the scroll bar of our mouse and print the output accordingly.

And pygame.QUIT functions let us exit the game whenever desired.

Thus we have seen how to detect mouse wheel movement.

Output:

Mouse wheel movement detected
Mouse wheel movement detected

Leave a Reply

Your email address will not be published.