Make OSX application respond to first mouse click when not focused
Asked Answered
D

3

16

Normal OSX applications eat the first mouse click when not focused to first focus the application. Then future clicks are processed by the application. iTunes play/pause button and Finder behave differently, the first click is acted on even when not focused. I am looking for a way to force an existing application (Remote Desktop Connection.app) to act on the first click and not just focus.

Dollar answered 24/9, 2008 at 15:55 Comment(1)
Your examples are not entirely correct. iTunes only supports click-through in a few places, like when the window is in mini-mode.Soundproof
J
14

Check NSView's acceptsFirstMouse, it may be what you're looking for.

acceptsFirstMouse: Overridden by subclasses to return YES if the receiver should be sent a mouseDown: message for an initial mouse-down event, NO if not.

  • (BOOL)acceptsFirstMouse:(NSEvent *)theEvent

Parameters theEvent The initial mouse-down event, which must be over the receiver in its window.

Discussion The receiver can either return a value unconditionally or use the location of theEvent to determine whether or not it wants the event. The default implementation ignores theEvent and returns NO.

Override this method in a subclass to allow instances to respond to click-through. This allows the user to click on a view in an inactive window, activating the view with one click, instead of clicking first to make the window active and then clicking the view. Most view objects refuse a click-through attempt, so the event simply activates the window. Many control objects, however, such as instances of NSButton and NSSlider, do accept them, so the user can immediately manipulate the control without having to release the mouse button.

Jobie answered 24/9, 2008 at 19:1 Comment(1)
Do you know if we can achieve this in SwiftUI?Sensor
P
1

Responding to the first mouse click when not focused is called 'click through'. And its worth is debated heatedly, for instance here and here.

Perilune answered 24/9, 2008 at 16:4 Comment(1)
This is irrelevant. When someone asks you where is Burger King you don't start to convince them to be vegetarian. There are people who are used to "click through". People who come form user friendly operating systems for example. In fact this could (and should) be a system setting. I don't know why it isn't, probably because of ego in Apple (they think they know everything better)Pileum
E
0

// Assuming you have 1 view controller that's always hanging around. Over ride the loadview. N.B. this won't work pre-yosemite.

- (void)loadView {
    NSLog(@"loadView");


    self.view = [[NSView alloc] initWithFrame:
                 [[app.window contentView] frame]];
    [self.view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

    int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
    trackingArea0 = [[NSTrackingArea alloc] initWithRect:self.view.bounds
                                                 options:opts
                                                   owner:self
                                                userInfo:nil];
    [self.view addTrackingArea:trackingArea0];


}
- (void)mouseEntered:(NSEvent *)theEvent {
    NSLog(@"entered");


        if ([[NSApplication sharedApplication] respondsToSelector:@selector(activateIgnoringOtherApps:)]) {
            [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
        }

}
Eluvium answered 7/2, 2015 at 1:3 Comment(2)
This is a, quite frankly, hopeless solution.Natator
You could try overriding the contentview of nswindow to make it less fragile.Eluvium

© 2022 - 2024 — McMap. All rights reserved.