Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another way would be to create a CGEventTap that captures mouse down events.<br> You then just have to remember the last event type and check that in one of your NSMenu delegate methods.<br> Here is some sample code:</p> <pre><code>CGEventRef MouseClickedEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon) { CGPoint location = CGEventGetLocation(event); switch (type) { case kCGEventLeftMouseDown: { NSLog(@"Left Mouse pressed at %@", NSStringFromPoint(NSPointFromCGPoint(location))); break; } case kCGEventRightMouseDown: { NSLog(@"Right Mouse pressed at %@", NSStringFromPoint(NSPointFromCGPoint(location))); break; } case kCGEventOtherMouseDown: { NSLog(@"Other Mouse pressed at %@", NSStringFromPoint(NSPointFromCGPoint(location))); break; } default: break; } return event; } - (void)applicationDidFinishLaunching:(NSNotification*)aNotification { CGEventMask eventMask = CGEventMaskBit(kCGEventLeftMouseDown)|CGEventMaskBit(kCGEventRightMouseDown)|CGEventMaskBit(kCGEventOtherMouseDown); CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, MouseClickedEventCallback, NULL); CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0); CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); CGEventTapEnable(eventTap, true); } </code></pre> <p>The <a href="http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html" rel="nofollow">Quartz Event Services docs</a> state that:</p> <blockquote> <p>Event taps receive key up and key down events if one of the following conditions is true:</p> <ul> <li>The current process is running as the root user. </li> <li>Access for assistive devices is enabled. In Mac OS X v10.4, you can enable this feature using System Preferences, Universal Access panel, Keyboard view.</li> </ul> </blockquote> <p>I double checked that and it seems that you really don't have to enable assistive devices to receive mouse events.</p> <p>But if <code>[[[NSApplication sharedApplication] currentEvent] buttonNumber]</code> does what you want, I'd definitely go with that. It's higher level and therefore less code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload