View Full Version : NSButton MouseHover

Apr 15, 2009, 08:21 AM
Does anybody know how can I get a mouse hover event. I want to handle this event because I want to change the image on the button when mouse is over it.

Any suggestions are welcome.

Apr 15, 2009, 08:31 AM
Setup a tracking rect/area for the view. On 10.5+ use addTrackingArea: and on 10.4+ use addTrackingRect:owner:userData:assumeInside:. Then the view will receive mouseEntered: and mouseExited: events which you can use to set the image.

Apr 15, 2009, 10:19 AM
that sounds gr8... will give it a try later today.

Apr 16, 2009, 05:15 AM
I am somehow not able to set tracking area for the view. Any ideas in which function I will have to do this and how?

My button is on NSTabView.

Apr 16, 2009, 11:29 AM
Make sure you're doing this inside a subclass of NSButton because you need to override the two mouse event methods, and you should setup the tracking rects inside the viewDidMoveToWindow method.

If you're still having trouble post your code.

Apr 16, 2009, 01:32 PM
Thanks a bunch.. I have tried the following on a simple project :

1. Made a new XCode Cocoa Project
2. In the default window added a button
3. Added Objective C controller class which contains referneces for the Button and the ContentView of the mainwindow.
4.Subclass NSButton.
5. Added the method viewDidMoveToWindow() and mouse entered and mouseexited methods to the NSButton subclass.

This is not working... game me some exception. Have I done something wrong here? Do I need to subclass view as well?

Later I subclassed mainWindow's ContentView and added mouseentered and mouseexited functions in it as well... but it did not work either :confused:

My code has become untidy after trying many things ... so I'll clean it and post it later.

Apr 16, 2009, 06:30 PM
Here's a little project that demonstrates this. Notice that in the nib the button's class is set to the NSButton subclass name.

Apr 16, 2009, 11:35 PM
My goodness. It was as simple as this. Thanks to you for this. :-)

Its working now.

Apr 17, 2009, 10:23 AM
I am now trying to implement this for all the buttons that I have in my app. For this I have taken following in the HoverButton.h class

NSString *normalImageName;
NSString *hoverImageName;

- (void)setNormalImageName:(NSString *)aString;
- (void)setHoverImageName:(NSString *)aString;

and the following in HoverButton.m class

- (void)setNormalImageName:(NSString *)aString
normalImageName = aString;
- (void)setHoverImageName:(NSString *)aString
hoverImageName = aString;

Also I have included Hoverbutton.h in the delegate class and m trying to set the image names in delegate.m

[button setNormalImageName:@"test.jpg"];


Here I have declared button as type of HoverButton.

I am gettting compile error "syntax error before '-' token in Hoverbutton.h.

Am I missing something here?

Apr 17, 2009, 10:32 AM
Well, I'm pretty sure HoverButton.cpp should be HoverButton.m otherwise the compiler will treat it as C++ code, not Objective-C.

Apr 17, 2009, 10:49 AM
Yes thanks for the correction.. the file is actually Hoverbutton.m and i made a mistake while typing.

Apr 17, 2009, 12:30 PM
If you're still getting the syntax error post your entire class, both the .h and .m files. If you copy and paste chunks of code that you think are related it usually is the wrong part :)

Also, unless you have GC enabled, those accessors are incorrect and should look something like this (it'd be better to replace them with @properties if you can):

- (void)setHoverImageName:(NSString *)aString
if (hoverImageName != aString) {
[hoverImageName release];
hoverImageName = [aString copy];

Apr 18, 2009, 12:01 PM
Figured out the reason of compile issue... and the code is working perfectly fine now.

Thanks for pointing out the flaw in the accessor body... these tips are so helpful :-)