Mouse Down events in Objective-C

前端 未结 3 1341
情书的邮戳
情书的邮戳 2021-01-20 12:10

I know this question has been asked a lot before, but nothing will work for me. The following code will not do anything at all.

- (void) mouseDown:(NSEvent*)         


        
相关标签:
3条回答
  • 2021-01-20 12:33

    Your subclass must have a parent class of NSResponder, otherwise you will not get any events.

    0 讨论(0)
  • 2021-01-20 12:33

    You're overriding the NSWindow class, you should be overriding the NSView "contentView" of the NSWindow class to capture mouse events. Most of the decorations (NSViews) on the window outside of the contentView are private.

    Just create a new NSView that overrides mouseDown, etc and add it as your content view to the NSWindow object.

    0 讨论(0)
  • 2021-01-20 12:36

    Make sure your class inherits from NSWindow and conforms to the <NSWindowDelegate> protocol. Otherwise, that's just a method that happens to be named mouseDown, and nobody will ever call it.

    Update: Change your header file so that it looks like this:

    @interface test : NSWindow <NSWindowDelegate> {  
    
    } 
    

    In other words, don't put a prototype of mouseDown inside the interface definition, or anywhere else in the .h file.

    In your implementation file (.m) put just the method:

    - (void) mouseDown:(NSEvent*)someEvent {         
        NSLog(@"It worked!");          
    } 
    

    Assuming that you have logging turned on in the device (are you sure you can read NSLog output from elsewhere in your program?), you should see "It worked!" printed there.

    I'm not an obj-C expert by any means, but I think by putting the mouseDown prototype inside the interface definition, you were basically creating your own custom mouseDown method which hid the "real" one. This indicated to the compiler that it should not call your mouseDown method on a window click.

    0 讨论(0)
提交回复
热议问题