I'm trying to have a Python program listen for keystrokes sent to a program that is launched as a subroutine (Popen).
The problem is that when the correct window is focused nothing happens. No acknowledgement that a key has been pressed. If I click off from the window to something else it works.
This listening is meant to debug a problem of not being able to send keystrokes to the program - pyautogui, pynput, keyboard, even Popen. communicate does not work. The keystrokes sent are ignored by the Python script, even if manually entered myself on the keyboard.
I'm running Retroarch through Lutris. My computer's OS is Ubuntu Linux 22.04, the display manager is Wayland. Below is the code:
from pynput import keyboard
import subprocess, time
def on_press(key):
print(key)
return False # stop listener; remove this if want more keys
lutris = f'LUTRIS_SKIP_INIT=1 lutris/bin/lutris lutris:rungameid/3'
p = subprocess.Popen(lutris, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
time.sleep(5)
listener = keyboard.Listener(on_press=on_press)
listener.start() # start to listen on a separate thread
listener.join() # remove if main thread is polling self.keys
I expect the cited block of code to wait for a keystroke, and exit once one is received.
If I have the Retroarch/Lutris window focused (active) all keystrokes are ignored.
Are you running on wayland or x? I can reproduce the issue in some way, on wayland I will get mous events when hovering over thunderbird, but not over firefox.
Can you fix it by setting $DISPLAY to the correct environment variable before launching lutris?
The pynput project states some platform limitations: