How do I handle a mousedown event inside a window in Cocoa?
如何在Cocoa窗口中处理mousedown事件?
My code:
我的代码:
-(void)mouseDown:(NSEvent *)event {
NSLog(@"yay");
}
I am using Mac OS10.6, in xcode 4.0.1.
我在xcode 4.0.1中使用Mac OS10.6。
EDIT: Yes, this is in the app delegate, but this is my .h:
编辑:是的,这是应用委托,但这是我的。h:
@interface jumperAppDelegate : NSWindow {
@interface jumperAppDelegate: NSWindow {
Which I have done before in app delegates (just not for mouse events). This is really annoying me
这是我之前在应用委托中做过的(只是不用于鼠标事件)。这真让我讨厌
3 个解决方案
#1
6
Make sure you inherit from NSWindow
, as well as conform to the <NSWindowDelegate>
protocol. Like this:
确保从NSWindow继承,以及符合
@interface YourWindow : NSWindow <NSWindowDelegate> {}
@end
Then you should receive the event notification.
然后您应该收到事件通知。
-(void)mouseDown:(NSEvent *)event {
}
#2
2
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.
对于这个被称为类的方法,需要从NSResponder继承。窗口和视图都是NSResponder的子类。如果您正在调用它的类不是NSResponder的子类,那么该方法将不会触发。
* Update * Also be sure to override acceptsFirstResponder to return yes.
* Update *还要确保重写acceptsFirstResponder以返回yes。
- (BOOL)acceptsFirstResponder {
return YES;
}
#3
0
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.
我不确定,但我听说在您的头文件(.h)中,您需要用“NSWindow”替换“NSObject”。我想测试一下,但我现在不在电脑前。
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.
使用“-acceptsFirstMouse方法。
-
Try creating an NSEvent:
试着创建一个NSEvent:
NSEvent * someEvent; -(void)mouseDown:(NSEvent*)someEvent;
NSEvent * someEvent;-(void)mouseDown:someEvent(NSEvent *);
This probably won't work, but I will have more information tomarrow
这可能行不通,但我将向你提供更多的信息
#1
6
Make sure you inherit from NSWindow
, as well as conform to the <NSWindowDelegate>
protocol. Like this:
确保从NSWindow继承,以及符合
@interface YourWindow : NSWindow <NSWindowDelegate> {}
@end
Then you should receive the event notification.
然后您应该收到事件通知。
-(void)mouseDown:(NSEvent *)event {
}
#2
2
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.
对于这个被称为类的方法,需要从NSResponder继承。窗口和视图都是NSResponder的子类。如果您正在调用它的类不是NSResponder的子类,那么该方法将不会触发。
* Update * Also be sure to override acceptsFirstResponder to return yes.
* Update *还要确保重写acceptsFirstResponder以返回yes。
- (BOOL)acceptsFirstResponder {
return YES;
}
#3
0
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.
我不确定,但我听说在您的头文件(.h)中,您需要用“NSWindow”替换“NSObject”。我想测试一下,但我现在不在电脑前。
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.
使用“-acceptsFirstMouse方法。
-
Try creating an NSEvent:
试着创建一个NSEvent:
NSEvent * someEvent; -(void)mouseDown:(NSEvent*)someEvent;
NSEvent * someEvent;-(void)mouseDown:someEvent(NSEvent *);
This probably won't work, but I will have more information tomarrow
这可能行不通,但我将向你提供更多的信息