2012-11-13 33 views
6

我想創建OS X應用程序,它顯示並聚焦在系統範圍的熱鍵,然後,使用相同的熱鍵,它應該消失並且切換焦點返回。就像阿爾弗雷德一樣。可可切換焦點到應用程序,然後將其切換回

問題是我不能回頭看以前使用的應用程序。通過回顧我的意思是我不能繼續在以前的應用程序中輸入。

這裏是我的熱鍵處理程序:

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

回答

5

在這兩種情況下也激活...你應該停用。在您激活,保存舊的活躍APP

 _oldApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; 

後激活

 [_oldApp activateWithOptions:NSApplicationActivateIgnoringOtherApps]; 

---完整的源

@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

這工作,但要注意的是,frontmostApplication是您的應用程序的時候applicationWilLBecomeActive通知到達。最重要的應用程序必須在熱鍵處理程序中進行檢查。 –

相關問題