Pete855217
Pete855217

Reputation: 1602

Cocoa - capturing specific events

I'm fairly new to Cocoa programming, and have a question about control event handling.

I create an 'action' for a button, and get an updated AppDelegate.m to handle this eg.

- (IBAction)seedBtnPressed:(id)sender {
   NSString* myString = @"Hi there";
   [_updateLbl setStringValue:myString];
}

When running this, pressing the 'seed' button does what it should - the label updates. My question is: why have I captured the 'button press event' by default, as I don't see any place where I've specified this. Alternately, how would I capture a mouse-over event with an action? I gather I'd create another action for the button, but am not sure how to specify this to handle 'mouse-over' events only? Sorry if I've used Windows terminology here, I understand Cocoa uses different names for things. Thanks Pete

Upvotes: 0

Views: 227

Answers (2)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

You need to Subclass the NSButton class (or even better the NSButtonCell class).

- (void)mouseEntered:(NSEvent *)theEvent;
- (void)mouseExited:(NSEvent *)theEvent;

They should get called when the mouse enter and exit the area. You may also need to re create the tracking area, look here:

- (void)updateTrackingAreas

For fade in and fade out effect I played with animator and alpha value for example:

[[self animator]setAlphaValue:0.5]; 

Upvotes: 1

rickerbh
rickerbh

Reputation: 9911

To get mouse-over events for an NSView you should use the NSTrackingArea class (assuming you're targeting a relatively modern version of OS X). Apple have good documentation on this available at http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/TrackingAreaObjects/TrackingAreaObjects.html

For your other query about the seedBtnPressed: triggering although you don't specify it - have you set an action in Interface Builder for the button rather than programmatically?

Upvotes: 1

Related Questions