moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.74k stars 244 forks source link

pynput detects mouse scroll wheel movements only *partially* in the Wayland window manager in Linux, but fully in X11 #555

Closed ElectricRCAircraftGuy closed 1 year ago

ElectricRCAircraftGuy commented 1 year ago

Description See below.

Platform and pynput version Ubuntu 22.04.2--brand new install. How do I check the pynput library version? I don't know how to do that.

Update: pip install pynput seems to show I have 1.7.6:

$ pip install pynput
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pynput in /home/gabriel/.local/lib/python3.10/site-packages (1.7.6)
Requirement already satisfied: python-xlib>=0.17 in /usr/lib/python3/dist-packages (from pynput) (0.29)
Requirement already satisfied: evdev>=1.3 in /home/gabriel/.local/lib/python3.10/site-packages (from pynput) (1.6.1)
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from pynput) (1.16.0)

To Reproduce

Originally posted here: https://github.com/albertz/mouse-scroll-wheel-acceleration-userspace/issues/8#issuecomment-1575389079

pynput works only partially in Wayland

I just confirmed there is also a problem with pynput. When running the Wayland window manager on Ubuntu 22.04, pynput can detect scroll wheel movements in Chrome just fine, but not in other programs such as the gnome-terminal, or Sublime Text.

Here is a small Python test program I just wrote:

# modified from here: https://pypi.org/project/pynput/

from pynput import mouse

def on_scroll(x, y, dx, dy):
    print('Scrolled {}; (x, y)=({:4d}, {:4d}); (dx, dy)={}'.format(
        'down' if dy < 0 else 'up  ',
        x, y,
        (dx, dy),
    ))

# block the main thread, listening
# Collect events until released
with mouse.Listener(
        on_scroll=on_scroll) as listener:
    listener.join()

And here is some sample output, which only prints a line when scrolling in Chrome, but not in my terminal nor in Sublime Text:

$ ./pynput_mouse_wheel.py
Scrolled down; (x, y)=(4625, 1695); (dx, dy)=(0, -1)
Scrolled down; (x, y)=(4625, 1695); (dx, dy)=(0, -1)
Scrolled down; (x, y)=(4625, 1695); (dx, dy)=(0, -1)
Scrolled down; (x, y)=(4625, 1695); (dx, dy)=(0, -1)
Scrolled down; (x, y)=(4625, 1695); (dx, dy)=(0, -1)
Scrolled down; (x, y)=(4625, 1695); (dx, dy)=(0, -1)
Scrolled down; (x, y)=(4625, 1695); (dx, dy)=(0, -1)
Scrolled down; (x, y)=(4625, 1695); (dx, dy)=(0, -1)
Scrolled up  ; (x, y)=(4505, 1698); (dx, dy)=(0, 1)
Scrolled up  ; (x, y)=(4505, 1698); (dx, dy)=(0, 1)
Scrolled up  ; (x, y)=(4505, 1698); (dx, dy)=(0, 1)
Scrolled up  ; (x, y)=(4505, 1698); (dx, dy)=(0, 1)
Scrolled up  ; (x, y)=(4505, 1698); (dx, dy)=(0, 1)
Scrolled up  ; (x, y)=(4505, 1698); (dx, dy)=(0, 1)
Scrolled up  ; (x, y)=(4505, 1698); (dx, dy)=(0, 1)
Scrolled down; (x, y)=(4505, 1698); (dx, dy)=(0, -1)
Scrolled down; (x, y)=(4505, 1698); (dx, dy)=(0, -1)
Scrolled down; (x, y)=(4505, 1698); (dx, dy)=(0, -1)

but it works fully in X11

But in the X11 Window manager, it works fully. My same program above is able to detect mouse scroll wheel movements in all programs, including Chrome, the terminal it's running in, Sublime Text, etc. That's a big difference and definitely a problem in Wayland!

ElectricRCAircraftGuy commented 1 year ago

Note: to switch between the X11 and Wayland window managers in Linux Ubuntu, see the "To use the X11 Window Manager in Ubuntu 20.04, 22.04, etc." section of my answer here: https://askubuntu.com/a/1109490/327339.

In short, log out, click your name, click the gear in the bottom-right, choose the X11 or Wayland window manager option.

I also mention that here: https://askubuntu.com/a/1470563/327339

moses-palmer commented 1 year ago

Thank you for your report.

This is a known issue, please see the documentation. In short, the security model of Wayland prevents this library from working, but you will notice partial support for applications running under XWayland, the X emulator. Notable examples of such applications are browsers derived from Chromium.