Cocoa switch focus to application and then switch it back

回眸只為那壹抹淺笑 提交于 2019-12-20 09:55:25

问题


I want to create OS X application which shows up and getting focused with system-wide hotkey, and then, with same hotkey it should dissapear and switch focus back. Just like Alfred does it.

The problem is that I can't focus back on application previously used. By focusing back I mean that I can't continue typing in previous app.

Here is my hotkey handler:

OSStatus OnHotKeyEvent(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData)
{
    AppDelegate *me = (__bridge AppDelegate*) userData;

    EventHotKeyID hkCom;

    GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hkCom), NULL, &hkCom);

    if([[me window] isVisible]) {
        [[NSApplication sharedApplication] activateIgnoringOtherApps:NO];
        [[me window] orderOut:NULL];
    }
    else {
        [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
        [[me window] makeKeyAndOrderFront:nil];

    }

    return noErr;
}

回答1:


well activate in both cases... you should deactivate. BEFORE you activate, save old active app

        _oldApp = [[NSWorkspace sharedWorkspace] frontmostApplication];

later activate that

        [_oldApp activateWithOptions:NSApplicationActivateIgnoringOtherApps];

--- full source

@implementation DDAppDelegate {
    NSStatusItem *_item;
    NSRunningApplication *_oldApp;
}

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
    NSLog(@"%@", [[NSWorkspace sharedWorkspace] frontmostApplication].bundleIdentifier);

    _item = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
    _item.title = @"TEST";
    _item.target = self;
    _item.action = @selector(toggle:);
}

- (void)applicationWillBecomeActive:(NSNotification *)notification {
    NSLog(@"%@", [[NSWorkspace sharedWorkspace] frontmostApplication].bundleIdentifier);
}

//---

- (IBAction)toggle:(id)sender {
    if(!_oldApp) {
        NSLog(@"%@", [[NSWorkspace sharedWorkspace] frontmostApplication].bundleIdentifier);
        _oldApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
        [NSApp activateIgnoringOtherApps:YES];
    }
    else {
        [_oldApp activateWithOptions:NSApplicationActivateIgnoringOtherApps];
        _oldApp = nil;
    }
}
@end


来源:https://stackoverflow.com/questions/13367703/cocoa-switch-focus-to-application-and-then-switch-it-back

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