Skip to content

Instantly share code, notes, and snippets.

@uberswe
Forked from ljos/cocoa_keypress_monitor.py
Last active August 29, 2015 14:09
Show Gist options
  • Save uberswe/041dc7e8af5b97e5a6fa to your computer and use it in GitHub Desktop.
Save uberswe/041dc7e8af5b97e5a6fa to your computer and use it in GitHub Desktop.
__author__ = 'MarkusTenghamn'
# cocoa_keypress_monitor.py by Bjarte Johansen is licensed under a
# License: http://ljos.mit-license.org/
# add the terminal application you run python from to application that are allowed to control your computer. This can be done in System Preferences > Privacy > Accessability.
from AppKit import NSApplication, NSApp
from Foundation import NSObject, NSLog
from Cocoa import NSEvent, NSKeyDownMask
from PyObjCTools import AppHelper
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
mask = NSKeyDownMask
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)
def handler(event):
try:
print event.keyCode()
except KeyboardInterrupt:
AppHelper.stopEventLoop()
def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
AppHelper.runEventLoop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment