Detect Mouse Scroll
Asked Answered
F

2

8

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?

Feodor answered 4/7, 2022 at 14:54 Comment(2)
pygame.org/docs/ref/mouse.html "In pygame 2, the mouse wheel functionality can be used by listening for the pygame.MOUSEWHEEL type of an event (Bear in mind they still emit pygame.MOUSEBUTTONDOWN events like in pygame 1.x, as well). When this event is triggered, a developer can access the appropriate Event object with pygame.event.get(). The object can be used to access data about the mouse scroll, such as which (it will tell you what exact mouse device trigger the event)." There's a code example too.Janicejanicki
For a MOUSEWHEEL event, look at event.y to see if you're scrolling up or down. Maybe that will account for missing events. It might be more likely that you're trying to process events in multiple places or something else. Please edit your question to include a minimal reproducible example so it's possible to assist you.Wandering
E
10

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)
Epanodos answered 4/7, 2022 at 15:5 Comment(0)
A
2
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

Autrey answered 5/5, 2024 at 1:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.