moses-palmer / pynput

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

Subclassing Listener results in win32_event_filter not working #354

Closed suurjaak closed 3 years ago

suurjaak commented 3 years ago

When subclassing Listener, the OS-specific keyword arguments like win32_event_filter no longer work.

It's because pynpyt.keyboard._base.Listener and pynpyt.mouse._base.Listener currently use self.__class__ to register keyword arguments as options:

prefix = self.__class__.__module__.rsplit('.', 1)[-1][1:] + '_'
self._options = {
    key[len(prefix):]: value
    for key, value in kwargs.items()
    if key.startswith(prefix)}

So if pynput.keyboard.Listener has been subclassed in user code, that self.__class__ is now from another module altogether instead of pynpyt.keyboard._win32, and thus win32_event_filter does not get registered in options.

Perhaps the argument handling could be achieved in another way that does not break when subclassing?

moses-palmer commented 3 years ago

Thank you for your report.

I suppose this could be solved in a different manner; perhaps with a class attribute. But what is your user case? Why would you want to subclass a listener rather than just encapsulate it?

suurjaak commented 3 years ago

Use case was keeping all mouse/keyboard event handling specifics in two thread instances that can be started and destroyed any number of times, forwarding their processed results to the owning object. Subclassing Listener to encapsulate input-specific event processing just seemed.. neater. Already provides a start-stop mechanism.