I'm using PyAutoGUI to listen for a keyboard shortcut, but when I press the shortcut keys, it triggers Google Chrome's Ctrl + F and Ctrl + O shortcuts to find in page and open a file.
However, I do not want it to trigger Chrome's shortcuts. I only want it to trigger a function in my code.
I'm using this:
keyboard.wait("ctrl + f + o", suppress=True, trigger_on_release=True)
But, unfortunately, it does not trigger the function in my code, and it also triggers Chrome's shortcuts (should not).
I want the shortcut to trigger only the function in my code. It shouldn't trigger the foreground window's (for example, Chrome's) shortcuts. How can I do this?
Yes, this is possible - but more complicated. You can use low-level keyboard hooks (just an example).
whereas perform_action() is the method you're calling once the shortcut is performed. Both
returnstatements are crucial as they signal whether they shortcut was executed or not.Now we have to implement the keyboard hook on a low-level:
Explanation:
FUNCspecifies the callback function via a void pointer. Important ishook = windll.user32.SetWindowsHookExA- with the four arguments provided:c_int(13)is the type of hook we are intending to set: type 13 equals toWH_KEYBOARD_LLwhich is a low-level keyboard hook!FUNCis the callback function that will be called when the keypress is detectedSo you can now use
keyboard.wait("ctrl + f + o", suppress = True, trigger_on_release = True)as long as you follow with the Unhook statement (for when the shortcut keys were actually pressed):windll.user32.UnhookWindowsHookEx(hook_id).The full code may be something along the lines of: