OSX/Cocoa:监听系统范围的鼠标拖动事件

4

你好,我是一名新手,想知道如何为系统级事件(例如鼠标拖动)创建一个监听器。我已将以下内容添加到我的应用程序中(我在另一篇帖子上看到的):

static CGEventRef eventFunction(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
    printf("event triggered\n");
    return event;
}

但它从未被调用,我不确定应该在何处注册回调函数。

1个回答

7

观察全局鼠标事件最简单的方法是使用 NSEvent 类方法 addGlobalMonitorForEventsMatchingMask:handler:

例如:

[NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDraggedMask 
                                       handler:^(NSEvent *event) {
    NSLog(@"Dragged...");
}];

请注意,这仅适用于其他应用程序,在您自己的应用程序中获取这些事件,您需要添加一个额外的本地事件处理程序。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接