0
我试图修改一个脚本来额外打印到日志文件。它已经使用NSLog()
。我肯定还在学习Python的...不管怎么说,这是我到目前为止有:写入NSLog和文件PythonObjC
# 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 sys
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
mask = NSKeyDownMask
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)
def handler(event):
try:
NSLog(u"%@", event)
with open("/Users/Zachary/Downloads/foo.txt", "a", 0) as myfile:
myfile.write(u"%@", event)
except KeyboardInterrupt:
AppHelper.stopEventLoop()
def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
AppHelper.runEventLoop()
if __name__ == '__main__':
main()
正如你所看到的,我试图通过myfile.write()
相同的数据NSLog()
,但是Python不喜欢这一点,我不知道如何正确地做。