From c842642c026a948257caf2a6e43a7f187ab6084e Mon Sep 17 00:00:00 2001 From: Asura <99897242+asur4s@users.noreply.github.com> Date: Fri, 24 Jun 2022 12:01:22 +0800 Subject: [PATCH] Replaced fake_input with display_manager's send_event --- pynput_service.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/pynput_service.py b/pynput_service.py index 382df9bed..24edcc1f9 100644 --- a/pynput_service.py +++ b/pynput_service.py @@ -6,6 +6,7 @@ import sys import socket from Xlib.ext.xtest import fake_input from Xlib import X +import Xlib KeyCode._from_symbol("\0") # test @@ -16,15 +17,31 @@ class MyController(Controller): :param event: The *X* keyboard event. :param int keysym: The keysym to handle. """ - event = X.KeyPress if is_press \ - else X.KeyRelease - keysym = self._keysym(key) - # Get keycode according to system language - keycode = self._display.keysym_to_keycode(keysym) + event = Xlib.display.event.KeyPress if is_press \ + else Xlib.display.event.KeyRelease + + origin_keysym = self._keysym(key) + keycode = self._display.keysym_to_keycode(origin_keysym) + + with display_manager(self._display) as dm, self.modifiers as modifiers: + # Under certain cimcumstances, such as when running under Xephyr, + # the value returned by dm.get_input_focus is an int + window = dm.get_input_focus().focus + send_event = getattr( + window, + 'send_event', + lambda event: dm.send_event(window, event)) + send_event(event( + detail=keycode, + state=self._shift_mask(modifiers), + time=0, + root=dm.screen().root, + window=window, + same_screen=0, + child=Xlib.X.NONE, + root_x=0, root_y=0, event_x=0, event_y=0)) - fake_input(self._display, event, keycode) - self._display.sync() keyboard = MyController()