I'm trying to make some application with python tkinter, that must always stay on the top of the screen. I searched a lot of documents, and I finally know how to set tkinter window to stay on the top, but couldn't find the way to stay top even though the user presses Win + D shortcut.
Is there any way to make tkinter window dismiss Win + D command?
You can set up a low-level keyboard hook to essentially "absorb" any forbidden keypresses, though it may be a bit overkill. In this example, I've set up a filter for the left and right Windows keys.
This relies on the pywin32 module, but could likely be rewritten to use
ctypesexclusively.For what it's worth, there are caveats:
This code is largely adapted from this answer, and the original author also provides a keyboard module that abstracts away some of this technical kung-fu - it's probably worth looking into.
With all that out of the way, the idea here is that you set up an instance of the
KeyFilterclass and target theKeyFilter.set_hookmethod to run in its own thread, and the keys you set up in theForbiddenKeysenum will be filtered out for as long as that thread (or more likely, your app which creates that thread) is running.There's a boilerplate example in the
if __name__ == '__main__'block so you can try running it directly to see how it works.