Make OSX application respond to first mouse click when not focused

安稳与你 提交于 2019-12-20 12:22:17

问题


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.


回答1:


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.




回答2:


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




回答3:


// 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];
        }

}


来源:https://stackoverflow.com/questions/128015/make-osx-application-respond-to-first-mouse-click-when-not-focused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!