pythonpygamemousewheel

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?


Solution

  • 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)