When I set suppress=True on pynput.keyboard.Listener and minimize my program window, it blocks the system from listening to the keyboard.
Is it possible to block the system from listening to the keyboar only when my program window is active?
When I set suppress=True on pynput.keyboard.Listener and minimize my program window, it blocks the system from listening to the keyboard.
Is it possible to block the system from listening to the keyboar only when my program window is active?
Copyright © 2021 Jogjafile Inc.
You can use
pywinctl, which works on Linux, MacOS, and of course Windows. Here I've incorporated thepynputlistener into a loop, where a function checks if the window is open or closed. Here's a snippet of code:This code checks whether the window is opened, if it is, then it suppresses keyboard input and listens for it. If not, the program does not suppress key presses, and does not listen for any keyboard input. Note that you need to replace the variable
program_window_namewith the name of your program, in my case, it is:'*IDLE Shell 3.12.1*'. To figure it out, run this simple test:Remember to keep your program the active window when you run this program. Then, you will see your window title. Simply replace the variable with the window title that you got from this program, and everything should work as planned.