pynput
pynput copied to clipboard
Pause listener while using Controller().type
Hi,
Here's some sample code I ran using version 1.7.6
from pynput import keyboard
def onPress(key):
print("key:" + key.char)
controller.type(key.char)
controller = keyboard.Controller()
listener = keyboard.Listener(on_press=onPress)
listener.start()
listener.join()
Every time the on_press function is ran, it calls the controller.type() function which basically duplicate every char typed.
Unfortunately, every time type() is called, it triggers on_press... and so on.
I found this issue which seems similar to my problem, but it says the problem was solved in an older version.
#40
Thank you for your report.
The issue---or rather, the solution to the issue---you linked was specific to to Windows. Since you have not used the bug issue template, I do not know on what platform you experience this.
On Windows, however, manual dispatch of generated events was required, since those events would not be detected in all cases. Other platforms, such as Xorg, however, provide no way of detecting whether an event is virtual or actually caused by a using typing on a physical keyboard.
This means that calling a Controller
from a Listener
callback is not generally possible, as it will cause an infinite feedback loop. To solve this, I have the branch feature-injected
open, but I have yet to find a way to detect virtual events under Xorg, so it has not been merged yet.
Thank you for your answer. I'd like to add that I am using Python 3 on Windows 10.
If I understand your message correctly, the issue shouldn't have happened on Windows ? And the branch feature-injected is trying to solve this issue on Linux ?
helo iam facing same probelm. i want to emulate right mouse button when left mouse button clicked that means when on click event callback called i want to press button how can i do it
@moses-palmer Friendly ping about this issue, any idea ?
if this bug cannot be solved then this library will be useless. please fix it
@lucasarnulphy , @keralaboy123 , see this for low-level suppression of some keys for Windows
Alternatively, try this util for automatic suppression of hotkeys
I have the same problem keyboard.Controller().type causes the on_press event to fire. I tried pausing the listener but the latter also block keyboard.Controller().type execution.
Asynchronous does not seem to be compatible with this library?
hi. calling a Controller from a Listener callback is a must have feature.but its not available. i have an solution to this problem.
when a process uses controller.press/click method then inside pynput set a variable as flag. and next time when pynput receive event in win32eventfilter then it should drop first keypress /release event because it is probably generated by itself.
i tried to implement it by using win32 event filter and directly used controller in it. but when i clicked key muliple time with in short time it crashed.
now i found a solution . use win32 eventfilter and start thread from listener and do click by controller in another thread.it worked