As far as I know, there is no Python library for this, so you are going to run your own APIs. The good news is that PyObjC (which comes with built-in Python on recent OS releases) often makes this easy.
There are two main options. For any of these actions, your application must have a Cocoa / CoreFoundation working environment (as on Windows, many things require you to have a “Windows GUI executable” rather than a “command line executable”), which I won’t explain how this to do. (Find a good tutorial for creating graphical Python applications if you don’t know how this happens because this is the easiest way.)
An easy option is the Cocoa Global Event Monitoring API. However, it has some serious limitations. You only get events that go to another application, which means that media keys, global hot keys and keys that are ignored for any reason will not be displayed. In addition, you need to "trust availability." (The easiest way to do this is to ask the user to enable it globally in the "Universal Access" panel in the "System Preferences".)
The tough option is the Quartz event tap API. This is much more flexible, and it only requires the appropriate rights (which, depending on the parameters you use, may include trust in accessibility and / or running as root), and it is much more powerful, but it requires much more work to get started, and you can ruin your system if you make a mistake (for example, you ate all the keystrokes and mouse events so that they never hit the OS, and you cannot reboot except the power button).
For links to all relevant functions, see https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/nsevent_Class/Reference/Reference.html (for NSEvent) and https: // developer .apple.com / library / mac / # documentation / Carbon / Reference / QuartzEventServicesRef / Reference / reference.html (for quartz events). A bit of googling should show a lot of code examples in Objective-C (for NSEvent) or C (for CGEventTap), but little or nothing in Python, so I will show a few small snippets that illustrate how you port samples for Python:
import Cocoa def evthandler(event): pass
Another option, at about the same level as Quartz events, are Carbon events (starting with InstallEventHandler). However, Carbon is deprecated, and it’s also harder to get from Python, so if you don’t have any specific reason for this, don’t do it.
There are several other ways to get to one point — for example, use DYLD_INSERT_LIBRARIES or SIMBL to insert some kind of code into each application, but I cannot think of anything that can be done in pure Python.