NSMenu mouse button pressed at 10.5

I am updating (shortening?) An Ive application written for 10.6+ to work in 10.5+. Im struggling with capturing the currently pressed mouse button in the selector -(void)menuWillOpen:(NSMenu *);.

For 10.6+, Im uses [NSEvent pressedMouseButtons], which allows me to press a button outside the event stream. However, this does not exist in 10.5+ (it seems I need to call [theEvent buttonNumber].

How to capture the pressed mouse button (right or left):

  • Inside my NSMenu delegate
  • Preferably inside the selector -(void)menuWillOpen:(NSMenu *)menu
  • In the estate, which works in both 10.5+ and 10.6+

I really appreciate the help and I know that StackOverflow will help the new Objective-C programmer!

Thanks Dustin

+5
source share
2

, ( Nick Paulson ):

[[[NSApplication sharedApplication] currentEvent] buttonNumber]

ughoavgfhw , :

[[NSApp currentEvent] buttonNumber]

+6

- CGEventTap, .
NSMenu.
:

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

Quartz , :

, :

  • root.
  • . Mac OS X v10.4, , , , .

, .

[[[NSApplication sharedApplication] currentEvent] buttonNumber] , , . , , .

+2

All Articles