moses-palmer / pynput

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

Why can't pynput run normally in ubuntu-22.04.1-preinstalled-desktop-arm64+raspi system? #534

Open chenmonster opened 1 year ago

chenmonster commented 1 year ago

Environment

import time
from pynput import mouse, keyboard

def on_press(key):
    print('press', key)

def on_release(key):
    print('release', key)

def on_click(x, y, button, pressed):
    print('click', x, y, button, pressed)

if __name__ == '__main__':
    kb_listener = keyboard.Listener(on_press=on_press, on_release=on_release)
    kb_listener.start()
    mouse_listener = mouse.Listener(on_click=on_click)
    mouse_listener.start()

    kb = keyboard.Controller()
    kb.press(keyboard.Key.up)  # can listen
    kb.release(keyboard.Key.up)  # can listen

    time.sleep(30)
    kb_listener.stop()
    mouse_listener.stop()

Result

When I click with the mouse or input with the keyboard, the listening function is not executed, and the result is as follows

Screenshot from 2023-02-02 18-51-10

moses-palmer commented 1 year ago

Thank you for your report.

Do you run an XOrg or Wayland based desktop? Wayland does not provide a general input listening mechanism, so when running under that graphics system, only events sent to applications running under XWayland will be seen.

You may want to look into using the uinput backend. This is activated by setting the environment variable PYNPUT_BACKEND_KEYBOARD=uinput, but it requires that you run your application as root.