2012-11-13 1 views
6

Je veux créer une application OS X qui apparaît et se concentrer avec la touche de raccourci du système, puis, avec le même raccourci, il devrait disparaître et passer le focus. Tout comme Alfred le fait.Cocoa passer l'accent sur l'application, puis le rebrancher

Le problème est que je ne peux pas me concentrer sur l'application précédemment utilisée. En se recentrant, je veux dire que je ne peux pas continuer à taper dans l'application précédente.

Voici mon gestionnaire raccourci clavier:

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

Répondre

5

bien activer dans les deux cas ... vous devriez désactiver. AVANT d'activer, enregistrez ancienne application actif

 _oldApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; 

plus tard que activer

 [_oldApp activateWithOptions:NSApplicationActivateIgnoringOtherApps]; 

--- Source complète

@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 
+1

Cela fonctionne, mais notez que la frontmostApplication est votre application par le temps la notification applicationWilLBecomeActive arrive. Le foremostApplication doit être vérifié dans le gestionnaire de raccourcis clavier. –

Questions connexes