-
Notifications
You must be signed in to change notification settings - Fork 253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suppressing only hotkey events on Windows #170
Comments
Same issue brought me here. The event filter runs before key up and key down and doesn't get passed a handle to the listener. I don't know what I'm supposed to do with this. The docs mention calling self.suppress_event() but there's no self. Suppressing from the filter blocks the key up/down. The filter also gets an event message and a virtual key/scan code, but my key up/down routines are using KeyCodes, so the comparisons are all off. I can't find a good way to convert between them. Kind of lost. I just want to make a global hotkey that doesn't get passed to the active window. |
The current API does not lend itself to global hotkeys---the workarounds mentioned in other thread have the potential to work, but require the end user to write platform specific code for each platform they want to support. The correct solution from the perspective of this library would be to add an API for global hotkeys, since those API's already exist for the supported platforms. Until that has been implemented, however, the only way to achieve this is using the event suppression functionality. You are correct in your observation that the data passed to the filter is incompatible with what the event callbacks receive, so you will have to modify your checks accordingly. There is really no way around this, as the filter ha to be called before the event is fully translated. You do not have to make the listener instance global, but you do need to name or in the same scope add the filter function. |
Ok. Thanks for the update. I think I see how to make this work based on that info. |
Here's how I got around it
Might be a cleaner way, this works though |
Could you clarify what you mean, by embedding your solution in the test code I originally posted? stop_propagation is not a known existing variable. Same test code (non-working):
|
Apologies for the delay,
|
Dear lukakoczorowski, so you have an outer/global listener and an inner listener? I am trying to make sense of it... And the win32_event_filter comes between the on_press/release(key) and the inner listener and prevents any defined keys reaching the inner listener? |
PS: I tried your code and it worked. |
PPS: I went with a key that works, but now it says: |
Finally I got it to work!
... |
Still, RWin (= cmd_r) and print_screen will not work. |
Is True/False the only result the win32_event_filter can export? Because I also want it to report a variable... |
The caller of the filter only responds to Boolean return values. What other value would you like to return? The callback acts as a simple filter, so I cannot think of any other reasonable class of values. |
Thanks! It is possible to use several |
OK, found out it is possible to have more arguments linked to different variables in the filter. |
Ah, had to tweak the code in the arguments. Now it works. |
I'm on Windows. I want to use pynput to establish a hotkey (say, F1) while the foreground window is another program (say, Notepad). When the user presses F1, I do not want the foreground program to find out about that key press. But I want the Python script to be aware that F1 was pressed. If I press any other key, Notepad should receive the key as usual.
There are multiple threads claiming this is possible using the pynput.keyboard.Listener.__init__() win32_event_filter argument, possibly in combination with pynput.keyboard.Listener.suppress_event():
#163
#70
#47
After messing with this for two hours, I have no idea how to make it work. How is win32_event_filter supposed to help suppress hotkeys for the foreground window, while still having those keys be processed by the Python program?
The callback win32_event_filter sets up doesn't even have access to Listener.suppress_event(). Even if you use global variables to give it access, calling listener.suppress_event() from the win32_event_filter callback just causes the on_press and on_release callbacks to never be initiated.
(I also failed to set up a subclass which inherits from Listener and overwrites the suppress property inherited from AbstractListener.)
What is the correct way to do this? Maybe I am supposed to use only the win32_event_filter callback, and not use on_press or on_release at all?
I include this skeleton program to show the situation:
The text was updated successfully, but these errors were encountered: