get mouse event in cocoa window controller

donkey

How should I get the mouse event in the Cocoa window controller or I should try another way around?

I am designing a feature where the text field transforms into a big plus sign when the mouse is hovering over its area.

Boy van Amstel

I would recommend subclassing NSTextField and handling the events there. As trojanfoe said, it has mouse-handling built in. Plus the feature you describe sounds like something that you might use again, in the same app or another. Just setting the class to your custom NSTextField will save time.

It could look something like this:

DCOHoverTextField.h

#import <Cocoa/Cocoa.h>

/** An `NSTextField` subclass that supports mouse entered/exited events.
 */
@interface DCOHoverTextField : NSTextField

@end

DCOHoverTextField.m

#import "DCOHoverTextField.h"

@interface DCOHoverTextField()

/* Holds the tracking area for the `NSTextField`. */
@property (strong) NSTrackingArea *trackingArea;

@end

@implementation DCOHoverTextField

- (void)updateTrackingAreas {
    // Remove tracking area if we have one
    if(self.trackingArea) {
        [self removeTrackingArea:self.trackingArea];
    }

    // Call super
    [super updateTrackingAreas];

    // Create a new tracking area
    self.trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
                                                     options: NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways
                                                       owner:self
                                                    userInfo:nil];

    // Add it
    [self addTrackingArea:self.trackingArea];
}

- (void)mouseEntered:(NSEvent *)theEvent {
    // TODO: Change text field into a plus sign.
}

- (void)mouseExited:(NSEvent *)theEvent {
    // TODO: Change text field back into a regular text field.
}

@end

After creating your subclass, go into Interface Builder, select your NSTextField and change the class to the subclass you created.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i get mouse click event element in cocoa

How can I detect if mouse is down outside of window in cocoa?

Observing mouse event in ViewModel Swift (Reactive Cocoa 3.0)

Get window values under mouse

GLFW Mouse event lag with window drag

Wait for user close window to get values from NSTextField - Cocoa

How to get current active window using Swift and Cocoa

Get Kendo Window in the drag event

(Java) Get Mouse Coordinates Within Window

How to get the content in the mouse floating window?

How to get an "mouse over" event in kivy

How can i get a mouse move event?

Can I get the mouse position in OpenCV without a mouse event?

Simulate window, document, and mouse click event properties in Angular Jest testing

Simulating mouse-down event on another window from CGWindowListCreate

How to add parameters to a window event / mouse wheel in svelte

Why tkinter is calculating mouse event outside tkinter window?

How to get the mouse event of a mousedown event when I pass a parameter

How can I pass NSNib.Name to a window controller in Swift/Cocoa

Cocoa Xcode: Open Window Controller from AppDelegate Programmatically using Swift 4 for Mac?

Adding a new window in Cocoa

Cocoa window resize information

Simple drawing with mouse on cocoa swift

Get instance of main window controller in app delegate

QML - Can't get mouse released event when I don't accept mouse pressed event

How to get an close event when the window is closed?

How to get the click outside window event

Xdamage: get event on window content changes

Angular get height position on window scroll event