Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Mouse Scroll

How do I detect Mouse scroll up and down in python using pygame? I have created a way to detect it, but it doesn't give me any information about which way I scrolled the mouse as well as being terrible at detecting mouse scrolls where only 1 in 20 are getting detected.

for event in pygame.event.get():
    if event.type == pygame.MOUSEWHEEL:
        print("Mouse Scroll Detected.")

Any other ways I can detect mouse scrolls?

like image 346
Cell Avatar asked May 17 '26 09:05

Cell


2 Answers

The MOUSEWHEEL event object has x and y components (see pygame.event module). These components indicate the direction in which the mouse wheel was rotated (for horizontal and vertical wheel):

for event in pygame.event.get():
    if event.type == pygame.MOUSEWHEEL:
        print(event.x, event.y)
like image 93
Rabbid76 Avatar answered May 18 '26 23:05

Rabbid76


for event in pygame.event.get():
    if event.type == pygame.MOUSEWHEEL:

After this, use event.y == 1 for upward scroll, while event.y == -1 for downward scroll

like image 21
Cesar Alvarez Avatar answered May 18 '26 23:05

Cesar Alvarez