moses-palmer / pynput

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

pynput sending the keys to current working window, causing unwanted file changes or input for the shell. #378

Closed guangyu-shi closed 3 years ago

guangyu-shi commented 3 years ago

When the input_focus got changed(working on another window), pynput will send the keys to the current working window, which can cause the unwanted input for the shel ,or changed files; so I need to detect the input_focus change before sending the keys; hope pynput send the keys only to the concerned process by default or let users choose which window to receive the keys.

I've overrided _send_key at my_Controller; but '\n' is still sent to the active window; so I need to listen to FocusChangeMask; yet can't detect the tab changing of xfce4-terminal.

import Xlib.display
from Xlib import X, Xatom

disp = Xlib.display.Display()
root = disp.screen().root

NET_ACTIVE_WINDOW = disp.intern_atom('_NET_ACTIVE_WINDOW')

window_id = root.get_full_property(NET_ACTIVE_WINDOW, Xlib.X.AnyPropertyType).value[0]
window = disp.create_resource_object('window', window_id)
window.change_attributes(event_mask=Xlib.X.PropertyChangeMask)
window.change_attributes(event_mask=Xlib.X.FocusChangeMask)

while True:

    window_name = window.get_wm_name()

    print('window_name =', window_name)

    event = disp.next_event()

    print('event =', event)

My host is running xubuntu linux. The keyboard is Xorg. pynput : 1.7.3 Linux : 4.15.0-136-generic

Bliss, --gs

moses-palmer commented 3 years ago

Thank you for your report, and sorry for this very late reply.

I think you will find it very difficult to be selective about what controls to target when using the keyboard controller. A long time ago, controls on windows would often correspond to X windows, but nowadays the widget toolkits only use Xorg as a dumb canvas; this is probably why you cannot detect tab changes in xfce4-terminal.

Targeting specific windows is out of scope for this library, so I will close this issue, but if you find a good solution, do not hesitate to post it here so that other might find it!