CocoaDev

Edit AllPages

Hey guys/girls,

I’ve been trying to get an NSView to respond to the mouseEntered event but having a lot of trouble. I’ve read apple’s docs and followed their steps but I think I must have missed something really quite obvious. Anyhow if it is possible, could someone paste some example code in or tell me the little trick in getting an NSView to respond to mouseEntered.

This is what I’ve got so far:

My window does accept mouse moved events and also does not ignore them. Here is the .m file of my window subclass

#import “mouseServer.h”

@implementation mouseServer

//In Interface Builder we set CustomWindow to be the class for our window, so our own initializer is called here.

@end

My custom view has had a trackingRect assigned, does accept first responder and has the method -(void)mouseEntered:(NSEvent *)theEvent Here is the .m file of my custom view subclass

#import “mouseServerCV.h”

@implementation mouseServerCV

-(void)mouseEntered:(NSEvent *)theEvent { NSLog(@”VIEW LOGGED”); } @end

BTW, if your wondering why i’m doing this, its because I need to create a hotcorner for my app. I achieved the correct behavior before by using applescript studio and having a small transparent window corner in the corner of the screen. If you think I might be wasting my time doing it this way and know of a better or more civilised way of creating hotcorner functionality please feel free to suggest!

Thanks!

Cheers Louis


At the time initWithFrame: is called, your view hasn’t been added to the window yet. Since it’s the window that keeps track of tracking rects, wait until viewDidMoveToWindow is called before calling addTrackingRect:.


One of your problems is your initWithContentRect:::: method – you never set self to result, and so you don’t call setAcceptsMouseMovedEvent: and setIgnoresMouseEvents: on the right object. Here’s (theoretically) correct version of the method, which I hope works out for you. –JediKnil


Wow! That worked a treat. Placing the custom window, and custom view in any corner of the screen + 1 pixel in height and width gives you an instant hotcorner. Thanks everyone! Cheers, Louis —-