Lefora Free Forum
Loading
1643 views

Cannot intercept mouseUp mouseDown events

Page 1
1–5
rookie - member
7 posts

I'm an experienced Windows C/C++/C# programmer just getting into Cocoa/XCode.  I have read through the Cocoa Event-Handling Guide and Googled many many hours for code examples and explanations, but I still can't get the code working to intercept mouse events (mouseUp/Down/Dragged/Moved).   I've attempted to override acceptsFirstResponder and becomeFirstResponder as specified in numerous places to no avail. I know there's one last little detail that I'm missing, but I can't find it.

Below is the source code for an extremely simple project called MouseTest, whose only function is to react to mouseDown.  The main window contains a single Image Well.  The code seems to follow all of the documentation for intercepting mouse events, but nothing happens.

Any help will be greatly appreciated.

Note: Contents of source files shown below are separated by lines of //===============================================

// FILE MouseTest-Info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleExecutable</key>
    <string>${EXECUTABLE_NAME}</string>
    <key>CFBundleIconFile</key>
    <string></string>
    <key>CFBundleIdentifier</key>
    <string>com.yourcompany.${PRODUCT_NAME:rfc1034identifier}</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>${PRODUCT_NAME}</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSMinimumSystemVersion</key>
    <string>${MACOSX_DEPLOYMENT_TARGET}</string>
    <key>NSMainNibFile</key>
    <string>MainMenu</string>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
</dict>
</plist>

//===========================================================================
// FILE  MouseTestAppDelegate.h

#import <Cocoa/Cocoa.h>

@interface MouseTestAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
}

@property (assign) IBOutlet NSWindow *window;

@end

//===========================================================================
//  FILE MouseTestAppDelegate.m

#import "MouseTestAppDelegate.h"

@implementation MouseTestAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
}

@end

//===========================================================================
//  FILE main.m

#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[])
{
    return NSApplicationMain(argc,  (const char **) argv);
}

//===========================================================================
//  FILE MouseTest.h
#import <Cocoa/Cocoa.h>
#import <AppKit/AppKit.h>

@interface MouseTest : NSView {

}
@end

//===========================================================================
//
// FILE MouseTest.m

#import "MouseTest.h"

@implementation MouseTest

- (BOOL)acceptsFirstResponder
{
    return YES;
}

- (BOOL)becomeFirstResponder
{
    return YES;
}

- (void)mouseDown:(NSEvent *)e
{
    NSRunAlertPanel(@"TEST", @"MouseDown",@"OK", nil, nil);
}

@end





?
63 posts

Buy Aaron Hillegass' "Cocoa Programming for Mac OS X" – the best Cocoa book there is. http://qgf.in/vlXSu4 He explains mouseStuff events.

__________________
kompilesoft

come and see my website! it's real cool reviews
?
288 posts






You need to subclass the NSView class (I've called in MyView and it's in MyView.m/h).  Use the File/New Wizard and he'll write you all the magic needed.
When you drop the NSView onto the UI in Interface Builder, set the class property in IB to MyView.
Then you're up and running.  add the following method to MyView and you're tracking the mouse.

- (void) mouseDragged : (NSEvent *) theEvent

{

   NSLog(@"mouseDragged x,y = %f %f",[theEvent deltaX],[theEvent deltaY]);
}

I attach the code (WhatADrag.zip).  If you can't get your code to work, please zip it up, post it and I'll fix it for you.  Please delete the build directory.  The zip should be about 50k (usually less)



There's also a mysterious method in NSControl class setEnabled:BOOL which says if mouse events are to be sent to the control.  I think it's OFF by default, however I think NSView sets that true for you.  However when a control is "mouse silent", that magic will get him talking!


I agree with K that you should get the book.  Cocoa's not easy to learn - however it is easy to use once learned!  I've also published some tutorials:  http://clanmills.com/articles/cocoatutorials/ 

Attachment: WhatADrag.zip (25.0KB)

__________________
rookie - member
7 posts

Clanmills, thanks for your help.  I bought the book, but it didn't have the answer I needed.  However, your example WhatADrag did the trick.  My project and WhatADrag were almost identical, except that mine didn't work.  So what I did was go through every window in XCode and IB and compared the two projects. When I clicked on my interface window in IB, it showed the class as NSView, whereas yours showed MyView.  As soon as I changed the class to my custom class, things started to work.  Onward and upward.

(What I don't like about working with Cocoa is these secret/magical little gizmos that live behind the scenes.  In my work with Microsoft Visual Studio C/C++/C# I'm able to view all pieces of the project -- mostly in easy-to-understand C-code -- and even make manual corrections.  It appears that in Cocoa I would have to understand all of the .xib XML tags to make any corrections -- a formidable-looking task at this point.)

Thanks again.

?
288 posts


Hi B

I'm really pleased you've got the book.  This gives us "shared currency" with which to discuss issues.

I am a veteran of 15 years of MFC (and 20 years of Windows all the way back to Windows 3.0).  So I understand what you're saying.

The nib/xib file is a mysterious object.  It is of course does the same as the Windows .rc/.res file - however he has more secret magic to handle bindings and stuff.  More pain awaits.

However trust me, you will come to like Cocoa - especially the Obj/C run-time.  And I'll be very happy to help your along the road to "Cocoa Enlightenment".  You only have to ask.

__________________
Page 1
1–5

Locked Topic


You must be a member to post in this forum

Join Now!