我有一个python脚本,用于侦听鼠标单击全局事件:
from AppKit import *
from PyObjCTools import AppHelper
import CoreFoundation
def handler(event):
NSLog(event)
print(event)
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSLeftMouseDownMask, handler)
AppHelper.runConsoleEventLoop()
但是,这只是永远运行,不响应鼠标事件,有什么问题吗
有两件事不对:
PyObjCTools API docs提供了许多示例代码。您可以查看HelloWorld脚本以了解如何创建应用程序对象。您需要将调用
addGlobalMonitorForEventsMatchingMask_handler_
的原始代码放在applicationDidFinishLaunching
回调中我使用的是macOS 10.13.6和Python3.7,我通过鼠标左键点击得到:
要在终端/控制台上运行脚本,它必须是后台任务
你可以稍后用
ps
杀死它:相关问题 更多 >
编程相关推荐