moses-palmer / pynput

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

[Support / Feature request] Suppress events during Controller committing actions to avoid duplicate events #570

Open Source61 opened 10 months ago

Source61 commented 10 months ago

Python version: 3.11.2 OS: Debian 12 Pynput version: 1.7.6

I'm having the issue that when I set a mouse controller to perform certain sequences of actions with monitoring (which is required to get the mousePos when left and right buttons are clicked) enabled, that events are duplicated for each controller action, i.e. I left click once, the monitoring is triggered which calls a function that moves somewhere and left click somewhere, now the monitoring is triggered again even though the input was from the controller, not a person.

Is it possible to suppress monitoring events triggered by a controller?

I tried using a block variable that is set on/off, but because of threading this variable is ignored / not assigned correctly across threads.

SpecialCharacter commented 10 months ago

Variable set undulatingly on/off worked for me in my program... [on_press(key) / on_release(key)].

Source61 commented 10 months ago

Variable set undulatingly on/off worked for me in my program... [on_press(key) / on_release(key)].

No idea what that's supposed to mean.

I don't see any documentation about any variable setting functions or methods.

Using a global variable to try to block the event does NOT work.

That said, I figured out why part of my script was not working. Mouse Controller object's position variable is unreliable. Gotta rely on (x, y) instead when assigning position values.

The event is still being triggered, with no way of suppressing it, which seems like a bug or a huge missing feature, but at least I got my script partially working.

thisthingrighthere commented 9 months ago

Can confirm that it has the same problem with the keyboard. I think @SpecialCharacter is suggesting turning reading on and off on each key press which seems like it would have a lot of overhead.

SpecialCharacter commented 9 months ago

@thisthingrighthere If you have an idea how to avoid this overhead, I am all ears :)

@Source61 Sorry for keeping you waiting. Undulating variable is echo:

        if chars == 'A':
            if echo == False or echo == True:
                echo = chars
                if ...
                else: ...
            else:
                echo = False
                pass
moses-palmer commented 8 months ago

Thank you for your report.

@Source61, as you duly note this is a huge missing feature. The long-living branch feature-injected attempts to resolve this. Completing the feature for your backend of choice, Xorg, was the blocker.

Reading this report brought me to look at the feature again. Running xev, events appear to be designated as synthetic when faked, and reading the xev source code revealed what field of the event was responsible for this output.

I have now updated the branch. Is it possible for you to test it? If you update, a new argument will be available for event listeners after the current argument list: injected: bool. It is supposed to be set for fake events. pynput will handle an event callback argument list without this argument specified.

SpecialCharacter commented 8 months ago

If this works, can you please backport it?