View Full Version : How to mouseonhover event in objective-c

Sep 5, 2008, 12:30 AM
I'm new to xcode/cocoa and objective-c programming and was looking for a onMouseHover event for my application. I've looked through the documentation and it doesn't seem to have one.

How do I go about programming this?

I've created a subclass of NSView but don't know where to go from there.


#import <Cocoa/Cocoa.h>

@interface subClass : NSView

- (void)mouseMoved:(NSEvent*)theEvent;


#import "subClass.h"

@implementation subClass

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
return self;

- (void)mouseMoved:(NSEvent*)theEvent {
[text setStringValue:@"mouse moved"];


Is there a way to add a mouse event using Interface builder just like how an 'action' or 'outlet' can be instantly added?

Also, do all actions execute on a leftmouseclick event? Is there a way to override this?

Sep 5, 2008, 08:21 AM
What exactly is the problem? If mouseMoved: isn't getting called you probably need to call setAcceptsMouseMovedEvents:YES on the view's window.

I'd suggest looking at the documentation for NSResponder. All views inherit from this.

Sep 5, 2008, 10:21 AM
Look at the mouseEntered: & mouseExited: methods (inherited from NSResponder), they should do what you want.

(Note - Mac apps generally don't change state on mouse-over to the same extent Windows & web apps do. Be wary of doing too much on mouse-over.)