Conor Taylor
Conor Taylor

Reputation: 3108

How do I handle a mousedown event inside a window in Cocoa

How do I handle a mousedown event inside a window in Cocoa?

My code:

-(void)mouseDown:(NSEvent *)event {
    NSLog(@"yay");
}    

I am using Mac OS10.6, in xcode 4.0.1.

EDIT: Yes, this is in the app delegate, but this is my .h:

@interface jumperAppDelegate : NSWindow {

Which I have done before in app delegates (just not for mouse events). This is really annoying me

Upvotes: 3

Views: 6692

Answers (3)

sosborn
sosborn

Reputation: 14694

For this method to be called the class it is being called in needs to inherit from NSResponder. Windows and views are all subclasses of NSResponder. If the class you are calling this from is not a subclass of NSResponder then the method will not fire.

* Update * Also be sure to override acceptsFirstResponder to return yes.

- (BOOL)acceptsFirstResponder {
   return YES;
}

Upvotes: 2

Justin
Justin

Reputation: 2142

I don't know for sure, but I have heard that in your header file (.h) that you need to replace the "NSObject" with "NSWindow". I would test it but I am not at my computer right now.

Also, make sure that you put the following code into your header file:

- (void) mouseDown:(NSEvent*)event;

EDIT: I have done some tests and research, but I cannot get it to work. I have two tips though.

  • Use the '-acceptsFirstMouse method.

  • Try creating an NSEvent:

    NSEvent * someEvent; -(void)mouseDown:(NSEvent*)someEvent;

This probably won't work, but I will have more information tomarrow

Upvotes: 0

sudo rm -rf
sudo rm -rf

Reputation: 29524

Make sure you inherit from NSWindow, as well as conform to the <NSWindowDelegate> protocol. Like this:

@interface YourWindow : NSWindow <NSWindowDelegate> {}
@end

Then you should receive the event notification.

-(void)mouseDown:(NSEvent *)event {    
}

Upvotes: 6

Related Questions