macostemplatesxcode5event-hooking

OSX global mouse/trackpad hooking


I am not familiar with Apple's OSX What I want to do is to set a global(system-wide) hook for 4 fingers scrolling (mouse and trackpad) and to be able to change the scrolling events (to make it more iOS like) because system preferences does't cover it. yes I assume there is a lot of programs like that but I want to make it my self (to learn OSX programming more).

My question is: What is the best template in Xcode to do so (there is many templates to start with and I have read about them , but I still can't understand which one is the best for it).

My question maybe a bit silly but I hope it is on topic for SO.

Thank you in advance. :)


Solution

  • Well the template you want to start with is OS X -> Application -> Cocoa Application

    Then having this in your AppDelegate.m is a good place to start, as far as hooking into global mouse/trackpad events:

    - (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;
    }