How to mouseonhover event in objective-c

Discussion in 'Mac Programming' started by xcode99, Sep 4, 2008.

  1. xcode99 macrumors newbie

    Sep 4, 2008
    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?
  2. kainjow Moderator emeritus


    Jun 15, 2000
    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.
  3. whooleytoo macrumors 603


    Aug 2, 2002
    Cork, Ireland.
    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.)

Share This Page