Skip to content

Instantly share code, notes, and snippets.

@tfhartmann
Created December 1, 2014 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfhartmann/2fb72184c7c1253e81c1 to your computer and use it in GitHub Desktop.
Save tfhartmann/2fb72184c7c1253e81c1 to your computer and use it in GitHub Desktop.
cocoa_keypress_monitor.py with logging to local example.log file
#!/usr/bin/env python
# cocoa_keypress_monitor.py by Bjarte Johansen is licensed under a
# License: http://ljos.mit-license.org/
from AppKit import NSApplication, NSApp
from Foundation import NSObject, NSLog
from Cocoa import NSEvent, NSKeyDownMask
from PyObjCTools import AppHelper
import logging
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
mask = NSKeyDownMask
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)
def handler(event):
try:
#NSLog(u"%@", event)
logging.info(event)
except KeyboardInterrupt:
AppHelper.stopEventLoop()
def main():
logging.basicConfig(filename='example.log', format='%(asctime)s %(message)s', level=logging.INFO)
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