Lauren
Lauren

Reputation: 1500

Capture other mouse button events on OS X with NSEvent

I'm trying to capture mouse clicks on OS X Yosemite.

From what I've read this code should work:

[NSEvent addGlobalMonitorForEventsMatchingMask:NSOtherMouseDown
    handler:^(NSEvent *event) {
       NSLog(@"Mouse Down, Button: %ld", event.buttonNumber);
    }];

However, it only works for events on the right mouse button, not other buttons (i.e. buttons 4, 5, 6, et cetera on multi-button mice).

Using Quartz Event Taps I'm able to detect mouse clicks on these buttons, like so:

CGEventTapCreate(kCGSessionEventTap,
                 kCGHeadInsertEventTap,
                 0,
                 CGEventMaskBit(kCGEventOtherMouseUp) | CGEventMaskBit(kCGEventOtherMouseDown),
                 MouseEventCallback,
                 NULL);

But I'd prefer to use the NSEvent API as it is more straightforward.

Any ideas on how to capture other mouse button events with NSEvent?

Upvotes: 0

Views: 954

Answers (1)

Daniel
Daniel

Reputation: 231

It's possibly just a typo in your question ?, but your code is missing the NSEventMaskFromType, e.g. :

[NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskFromType(NSOtherMouseDown) ..
                                               ^^^^^^^^^^^^^^^^^^^

Upvotes: 1

Related Questions