我正在尝试监视我的Macbook上的按键,以便构建一个统计分析程序。但我如何才能将字符与“event”分离,后者更像是:
NSEvent: type=KeyDown loc=(850,248) time=66551.8 flags=0x100 win=0x0 winNum=0 ctxt=0x0 chars="l" unmodchars="l" repeat=0 keyCode=37
那么,是否有人知道如何根据下面发布的脚本,使用chars(来自NSEvent)值实现一个.txt文档?我需要一个带有按键的文本文件,以便在上面运行我的其他脚本并分析频率等。。。
提前感谢;)
#!/usr/bin/python
# -*- coding: utf-8 -*-
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
except keyboardInterrupt:
AppHelper.stopEventLoop()
def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
AppHelper.runEventLoop()
if __name__ == '__main__':
main()
用于使用键码模块。从这里克隆它并运行“sudo setup.py install”。您将拥有钥匙码模块 https://github.com/abarnert/pykeycode
我在github用户的分支-https://github.com/gurgeh/selfspy/blob/new_activity/sniff_cocoa.py中找到了github用户对selfspy的OSX修改。
我把自己的一些想法放进去,这就是结果。感谢gurgeh找出了如何捕获窗口更改事件-这使得密钥记录器更加有趣,因为您基本上可以忽略不提供有趣事件的应用程序,或者对于具有统计头脑的人,列出您使用最多的应用程序以及您在其中所做的事情。。
相关问题 更多 >
编程相关推荐