Why does my clickable box needs a double click to have a clickcount that is equal to 1?

716 views Asked by At

I have an NSStatusItem that has an NSMenuItem which contains a custom NSView. this NSView contains 3 Subviews of NSBox which are clickable (implemented the mouseDown event). Strangely, when I run the application and I enter the statusItem right after I pressed the 'Play'-Button I have to click the NSBox just one time (this returns clickCount = 1). When I enter another window or view and I go back to the statusItem and I try to click one of the NSBoxes nothing happens. When I double click the items, the actions happen and the clickCount is also equal to 1.

-(void)mouseDown:(NSEvent *)theEvent {
    NSLog(@"ClickCount: %ld", theEvent.clickCount);
    if ([delegate respondsToSelector:@selector(boxClicked:)]) {
        [delegate boxClicked:self];
    }
}

Does anyone have any idea why this is happening and how I can solve this?

EDIT (full m-file code):

#import "ClickableBox.h"

@implementation ClickableBox

@synthesize delegate;

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
    return YES;
}

- (void)viewDidMoveToWindow {
    [self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:NO];
}

-(void)mouseEntered:(NSEvent *)theEvent {
    if ([delegate respondsToSelector:@selector(boxRolledOver:)]) {
        [delegate boxRolledOver:self];
    }
}

-(void)mouseExited:(NSEvent *)theEvent {
    if ([delegate respondsToSelector:@selector(boxExited:)]) {
        [delegate boxExited:self];
    }
}

-(void)mouseDown:(NSEvent *)theEvent {
    NSLog(@"ClickCount: %ld", theEvent.clickCount);
    if ([delegate respondsToSelector:@selector(boxClicked:)]) {
        [delegate boxClicked:self];
    }
}

- (void)dealloc {
    [delegate release];
    [super dealloc];
}

@end
2

There are 2 answers

1
Alex Zielenski On BEST ANSWER

This could be because your view is not key. You can set a delegate for your NSMenu and on -menuWillOpen: you can call [boxView.window makeFirstResponder:boxView]; to make it key.

0
petert On

It does sound like you also need to do this:

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
  return YES;
}

so that first mouse-click events are accepted in your custom NSView.

There may be other issues, but can't tell from the code-snippet as-is.