OSX / Cocoa: listening for system drag and drop events

Hey. I am new to cocoa programming and would like to know how to create a listener for system events (e.g. drag and drop). I added this to my application (I saw it in another post):

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

But it is never called and not sure where I should register the callback.

+4
source share
1 answer

The easiest way to view global mouse events is to use the NSEvent addGlobalMonitorForEventsMatchingMask:handler: class method addGlobalMonitorForEventsMatchingMask:handler:

Example:

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

Please note that this only works in other applications, in order to receive these events in your own application, you need to add an additional local event handler.

+7
source

All Articles