views:

33

answers:

1

I made a GUI with Tkinter, now how do I make it so when a key command will execute a command even if the Tkinter window is not in focus? Basically I want it so everything is bound to that key command.

Example:

Say I was browsing the internet and the focus was on my browser, I then type Ctrl + U. An event would then run on my program.

+1  A: 

Tkinter, on its own, cannot grab keystrokes that (from the OS's/WM's viewpoint) were directed to other, unrelated windows -- you'll need to instruct your window manager, desktop manager, or "operating system", to direct certain keystrokes differently than it usually does. So, what platform do you need to support for this functionality? Each platform has different ways to perform this kind of functionality, of course.

Alex Martelli
I need it on Windows 7.
Anteater7171
So you could use the Windows 7 GUI to set up a custom shortcut key that will run a program (e.g. a Python script) which sends e.g. a keystroke combination of your choice to your Tkinter app, e.g. with sendkeys, http://www.rutherfurd.net/python/sendkeys/ -- once Tkinter receives the "keystroke combination of your choice" in question, it _can_ execute whatever command you've bound to that combination; the problem is only in getting that keystroke combo to the Tkinter app in the first place, when the normal focus is elsewhere.
Alex Martelli