Objective C, Where to put mouse event methods?

Go To StackoverFlow.com

0

I'm creating my first application and I have a window consisting of multiple subclasses for the views and the window. I have one NSWindowController class, one NSWindow subclass, and three NSView subclasses to make a single window. Everything is working perfectly and looks perfect except I need to use mouse events but I don't know where to put it. Actually I tried putting it in every class file and it still doesn't work..

What I want to do is to make my existing window, which will always be on top, to go transparent (not completely transparent but partially transparent and I know how to set it) when the mouse has exited the window, and only become opaque when the mouse has entered the window or when the window becomes the key window.

The following are the parts of the codes that might help understand what's going on: (I changed some of the variable and the class names)


App Delegate:

#import "AppDelegate.h"
#import "MainWindowController.h"

@implementation AppDelegate

@synthesize window = _window;

-(IBAction)showMainWindow:(id)sender
{
    mainWindowController = [[MainWindowController alloc] init];
    mainWindow = [mainWindowController window];
    [mainWindow makeKeyAndOrderFront:sender];
    [NSApp activateIgnoringOtherApps:YES];
}

.....

MainWindowController:

#import "MainWindowController.h"

-(id) init {
    self = [super initWithWindowNibName:@"MainWindow"];
    return self;
}

.....

// These don't work
-(void)mouseExited:(NSEvent *)theEvent
{
    if ([self.window level] == NSFloatingWindowLevel && ![self.window isKeyWindow]) {
        [self.window setOpaque:NO];
    }
}

-(void)mouseEntered:(NSEvent *)theEvent
{
    if ([self.window level] == NSFloatingWindowLevel) {
        [self.window setOpaque:YES];
    }
}

.....

And then I just have three NSView classes and an NSWindow subclass to make a round-rect window with colors. What/where should I modify/add to get the result that I want?

I also tried adding [window setAcceptsMouseMovedEvents:YES] but still didn't work.

Thanks in advance!

2012-04-04 22:22
by Dennis
Have you read the Handling Mouse Events section of the Cocoa Event Handling Guide? The documentation is your friend.. - Joshua Nozzi 2012-04-04 23:22
@JoshuaNozzi Yes but I still don't get it and that's why I asked. Especially about how to recognize if the mouse is on the window (or view) or not when the application is not focused. If there is a specific explanation about that in the mouse event documentation, could you please give me a little more hint where it is - Dennis 2012-04-05 00:47
Nevermind. I got it - Dennis 2012-04-05 01:00


0

The -mouseExited: and -mouseEntered: methods should be put in an NSView subclass. Use NSTrackingArea(s) in the view(s) to be notified of mouse entered, moved and exited events. See the NSTrackingArea documentation for more information.

2012-04-04 22:52
by Andrew Madsen
Ads