OSX全球鼠标/触控板挂钩

时间:2014-03-21 02:42:25

标签: macos templates xcode5 event-hooking

我不熟悉Apple的OSX 我想要做的是为4个手指滚动(鼠标和触控板)设置一个全局(系统范围)挂钩,并且能够 更改滚动事件(使其更像iOS),因为系统首选项不会覆盖它。 是的我假设有很多这样的程序,但我想让它成为我自己(更多地学习OSX编程)。

我的问题是:Xcode中最好的模板是什么(开始时有很多模板,我已经阅读过它们,但我仍然无法理解哪一个最适合它)。

我的问题可能有点傻,但我希望它是关于SO的主题。

提前谢谢你。 :)

1 个答案:

答案 0 :(得分:2)

你想要开始的模板是OS X - >申请 - >可可应用

然后在你的AppDelegate.m中拥有它是一个很好的起点,就连接到全局鼠标/触控板事件而言:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMaskForAllEvents, handleCGEvent, (__bridge void *)(self));
    CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
    CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopCommonModes);
    CGEventTapEnable(eventTap, true);
}

CGEventRef handleCGEvent(CGEventTapProxy proxy, CGEventType type, CGEventRef eventRef, void *refcon) 
{
    if (type == kCGEventLeftMouseDown /*|| type == kCGEventMouseMoved || type == kCGEventMouseDragged || ...*/) {

    }

    return eventRef;
}