moses-palmer/pynput

Unexpected keyboard.type behavior on Linux

ricky-say-more opened this issue · 3 comments

keyboard.type does not work as I would expect when in the "Activities overview" within the Gnome desktop environment. The "Activities overview" is the state you get to when hitting the super key while using Gnome. It includes a search bar at the top where you can search applications and currently opened windows. Pynput seems to "send" keystrokes to the last active window rather than this search bar. Here is an experiment that illustrates the problem for me on Ubuntu 20.04 (you should tap <super> right after running so that you are in the "Activities overview" when keyboard.type runs):

from pynput.keyboard import Controller
keyboard=Controller()
from time import sleep
sleep(5); keyboard.type("hello world") # tap <super> right after executing this line

Also, I thought that simulating <ctrl>-<alt>-t with Pynput would open a new terminal, but it doesn't. I'm wondering what I need to know to understand Pynput's behavior in these situations and if there's anything I can do to get the intended behavior.

I have never managed to send any events to the activities overview to be honest. Do you run pynput in an X or Wayland session? When running under Wayland, you can only interact with and listen to applications launched under Xwayland.

Depending on what you are trying to achieve, you might be able to find a D-BUS interface instead. D-Feet is one of the tools you can use to introspect services and interfaces available.

@moses-palmer I'm in an X session. Thanks, I'll check out D-Feet. Any idea why ctrl-alt-t doesn't trigger the gnome shortcut to open a new terminal?

Hey @moses-palmer and @ricky-say-more - was working on something else and I figured this out. Maybe @moses-palmer can help explain the difference and why they behave differently?

This does not work:

from pynput import keyboard
from pynput.keyboard import Controller, Key

kb = Controller()
kb.press(Key.ctrl)
kb.press(Key.alt)
kb.press("t")
kb.release("t")
kb.release(Key.alt)
kb.release(Key.ctrl)

But this does open up the gnome terminal

from pynput import keyboard
from pynput.keyboard import Controller, Key

kb = Controller()
kb.press(Key.ctrl)
kb.press(Key.alt)
kb.press(keyboard.KeyCode(0x74))
kb.release(keyboard.KeyCode(0x74))
kb.release(Key.alt)
kb.release(Key.ctrl)