Paul
Paul

Reputation: 234

NSEvent - NSLeftMouseDown

I'm trying to trigger basic functions using NSEvent and mouse clicks. For example close the window when pressing left mouse button. What else do I need in this method?

Thanks.

 - (void)mouseDown:(NSEvent *)theEvent {

if ([theEvent type] == NSLeftMouseDown){

    [window orderOut:nil];

  }
}

Upvotes: 1

Views: 1488

Answers (1)

Peter Hosey
Peter Hosey

Reputation: 96323

Assuming this is in a custom view and the window outlet is connected (or you fill in that variable with [self window] when the view is added to a superview), that should be all you need. I would suggest handling mouseUp: instead of mouseDown:, though, to give the user the opportunity to back out by moving the mouse outside of your view.

You might also consider using an NSButton instead of (or inside of) a custom view. You could hook it up directly to the window's performClose: or orderOut: action.

Upvotes: 2

Related Questions