Ramiro Araujo
Ramiro Araujo

Reputation: 475

NSEvent addGlobalMonitorForEventsMatchingMask not working on CLI app

I'm pretty newbie with Obj-C and Cocoa. I'm trying to develop a very simple command line app that on launch wait's for a few global mouse events, and quits when done. I've correctly implemented an NSRunLoop to avoid the cli app finishing right away, but I can't seem to grasp the event listening for mouse events.

I'm using this snippet:

[NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMovedMask handler:^(NSEvent *mouseEvent) {
    NSLog(@"Mouse moved: %@", NSStringFromPoint([mouseEvent locationInWindow]));
}];

I'm my CLI app it simply doesn't fire anything, though it compiles correctly, while it works just fine if pasted inside a blank Cocoa App, in the applicationDidFinishLaunching method in the appDelegate class.

What I'm I missing? Thanks!

pd: I'm not 100% sure it's a CLI what I need though. I could also be an invisible or headless application, without a window, menu item or anything related what so ever, just a process that runs for a few seconds until the criteria is met.

Upvotes: 3

Views: 1435

Answers (1)

Wil Shipley
Wil Shipley

Reputation: 9543

I would guess you need a running NSApplication to use NSEvent. You could make a headless, icon-less NSApp, I believe.

Upvotes: 2

Related Questions